Beispiel #1
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="byteData">文件流数组</param>
 /// <param name="bucketName">存储空间名称</param>
 /// <param name="key">文件名</param>
 /// <param name="customDmain">自定义域名</param>
 /// <param name="result">成功则返回文件地址否则返回错误信息</param>
 /// <returns>是否上传成功</returns>
 public bool PutObject(byte[] byteData, string bucketName, string key, string customDmain, out string result)
 {
     using (Stream fileStream = new MemoryStream(byteData))//转成Stream流
     {
         string md5 = Aliyun.OSS.Util.OssUtils.ComputeContentMd5(fileStream, byteData.Length);
         try
         {
             //将文件md5值赋值给meat头信息,服务器验证文件MD5
             var objectMeta = new Aliyun.OSS.ObjectMetadata
             {
                 ContentMd5 = md5,
             };
             //文件上传--空间名、文件保存路径、文件流、meta头信息(文件md5)
             client.PutObject(bucketName, key.TrimStart('/'), fileStream, objectMeta);
             if (string.IsNullOrEmpty(customDmain))
             {
                 result = "http://" + bucketName + "." + EndPoint + "/" + key; //默认网址
             }
             else
             {
                 result = customDmain.TrimEnd('/') + "/" + key; //自定义网址
             }
             return(true);
         }
         catch (Exception e)
         {
             result = e.Message;
             return(false);
         }
     }
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="objectName"></param>
 /// <param name="content"></param>
 /// <param name="overrideIfExists"></param>
 /// <param name="objectMetadata"></param>
 public void PutObject(string objectName, Stream content, bool overrideIfExists, ObjectMetadata objectMetadata = null)
 {
     if (objectName != null)
     {
         objectName = objectName.Trim('/');
     }
     if (String.IsNullOrEmpty(objectName))
     {
         throw new ArgumentNullException("objectName");
     }
     if (content == null || content == Stream.Null)
     {
         throw new ArgumentNullException("content");
     }
     try
     {
         if (!client.DoesBucketExist(this.bucketName))
         {
             client.CreateBucket(this.bucketName);
         }
         bool objExists = client.DoesObjectExist(this.bucketName, objectName);
         if (overrideIfExists || !objExists)
         {
             var metadata = buildOssMetadata(objectName, objectMetadata);
             client.PutObject(this.bucketName, objectName, content, metadata);
         }
     }
     catch (Exception ex)
     {
         throw new StorageIOException("Put object fail!", ex);
     }
 }