zoukankan      html  css  js  c++  java
  • OSS上传文件(一)

    引用AliyunOSS.dll

    封装一个AliyunOSS帮助类

    public static class AliyunOSS {
    static string accessKeyId = Config.AccessKeyId;
    static string accessKeySecret = Config.AccessKeySecret;
    static string endpoint = Config.Endpoint;
    static OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    static string fileToUpload = Config.FileToUpload;
    static double Expiration = Config.Expiration;
    public static void PutObjectFromFile(string bucketName, string key, string ContentType = "image/jpeg") {
    
    try {
    using (var content = File.Open(fileToUpload, FileMode.Open)) {
    var metadata = new ObjectMetadata();
    metadata.ContentLength = content.Length;
    metadata.ContentType = ContentType;
    client.PutObject(bucketName, key, content, metadata);
    }
    
    // Console.WriteLine("Put object:{0} succeeded", key);
    }
    catch (OssException ex) {
    // Console.WriteLine("Failed with error code: {0}; Error info: {1}. 
    RequestID:{2}	HostID:{3}",
    // ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    }
    catch (Exception ex) {
    // Console.WriteLine("Failed with error info: {0}", ex.Message);
    }
    }
    
    public static void PutObject(string bucketName, string key, Stream content, ObjectMetadata metadata) {
    try {
    client.PutObject(bucketName, key, content, metadata);
    }
    catch (OssException ex) {
    
    }
    catch (Exception ex) {
    
    }
    }
    public static void DeleteObject(string bucketName, string key)
    {
    try
    {
    client.DeleteObject(bucketName, key);
    }
    catch (OssException ex)
    {
    
    }
    catch (Exception ex)
    {
    
    }
    }
    
    public static void PutObject(string bucketName, string key, Stream content)
    {
    try
    {
    client.PutObject(bucketName, key, content);
    }
    catch (OssException ex)
    {
    
    }
    catch (Exception ex)
    {
    
    }
    }
    
    public static string PutLocalFileBySignedUrl(string bucketName, string key) {
    OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try {
    var request = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);
    request.Expiration = DateTime.Now.AddMinutes(Expiration);
    return client.GeneratePresignedUri(request).AbsoluteUri;
    
    }
    catch (OssException ex) {
    // Console.WriteLine("Failed with error code: {0}; Error info: {1}. 
    RequestID:{2}	HostID:{3}",
    // ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    return null;
    }
    catch (Exception ex) {
    // Console.WriteLine("Failed with error info: {0}", ex.Message);
    return null;
    }
    }
    public static bool DoesObjectExist(string bucketName, string key)
    {
    OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    int i = 0;
    GoConn:
    try
    {
    return client.DoesObjectExist(bucketName, key);
    }
    catch (OssException ex)
    {
    if (i < 3)
    {
    ++i;
    goto GoConn;
    }
    throw;
    }
    }
    }
  • 相关阅读:
    Java IO总结
    Tomcat处理一个HTTP请求的过程
    Tomcat的web项目部署方式
    Tomcat性能调优
    jquery基础知识汇总
    Javascript中的正则表达式
    HTTP首部
    HTTPS
    Javascript中关于cookie的那些事儿
    HTTP请求方法详解
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11613525.html
Copyright © 2011-2022 走看看