public async Task <bool> CopyObjectAsync(string bucketName, string objectName, string destBucketName, string destObjectName = null) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (string.IsNullOrEmpty(objectName)) { throw new ArgumentNullException(nameof(objectName)); } if (string.IsNullOrEmpty(destBucketName)) { destBucketName = bucketName; } if (string.IsNullOrEmpty(destObjectName)) { destObjectName = objectName; } bucketName = ConvertBucketName(bucketName); CopySourceStruct copySource = new CopySourceStruct(Options.Endpoint, bucketName, Options.Region, objectName); string bucket = ConvertBucketName(destBucketName); CopyObjectRequest request = new CopyObjectRequest(bucket, destObjectName); //设置拷贝源 request.SetCopySource(copySource); //设置是否拷贝还是更新,此处是拷贝 request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Copy); //执行请求 CopyObjectResult result = _client.CopyObject(request); return(await Task.FromResult(result.IsSuccessful())); }
/// 复制对象时保留对象属性 public void CopyObject() { //.cssg-snippet-body-start:[copy-object] try { string sourceAppid = "1250000000"; //账号 appid string sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶 string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域 string sourceKey = "sourceObject"; //源对象键 //构造源对象属性 CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, sourceRegion, sourceKey); string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 CopyObjectRequest request = new CopyObjectRequest(bucket, key); //设置拷贝源 request.SetCopySource(copySource); //设置是否拷贝还是更新,此处是拷贝 request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.Copy); //执行请求 CopyObjectResult result = cosXml.CopyObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } //.cssg-snippet-body-end }
/// 修改对象元数据 public void ModifyObjectMetadata() { //.cssg-snippet-body-start:[modify-object-metadata] try { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 string appId = "1250000000"; //账号 appid string region = "COS_REGION"; //源对象的存储桶所在的地域 //构造对象属性 CopySourceStruct copySource = new CopySourceStruct(appId, bucket, region, key); CopyObjectRequest request = new CopyObjectRequest(bucket, key); //设置拷贝源 request.SetCopySource(copySource); //设置是否拷贝还是更新,此处是拷贝 request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.REPLACED); // 替换元数据 request.SetRequestHeader("Content-Disposition", "attachment; filename=example.jpg"); request.SetRequestHeader("Content-Type", "image/png"); //执行请求 CopyObjectResult result = cosXml.CopyObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } //.cssg-snippet-body-end }
public void Rename(string oldname, string newname) { try { cosXml = GetCosXml(); string sourceAppid = _cosOptions.Appid; string sourceBucket = _cosOptions.Bucket; string sourceRegion = _cosOptions.Region; string sourceKey = GetFullKey(oldname); CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, sourceRegion, sourceKey); string bucket = _cosOptions.Bucket; string key = GetFullKey(newname); CopyObjectRequest request = new CopyObjectRequest(bucket, key); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), KEY_DURATION_SECOND); request.SetCopySource(copySource); request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.COPY); CopyObjectResult result = cosXml.CopyObject(request); //Console.WriteLine(result.GetResultInfo()); Delete(oldname); } catch (COSXML.CosException.CosClientException clientEx) { Logging.Log.WriteErrorMessage(LOGTAG, "Rename", clientEx, "Rename failed: {0} to {1}", oldname, newname); throw; } catch (COSXML.CosException.CosServerException serverEx) { Logging.Log.WriteErrorMessage(LOGTAG, "Rename", serverEx, "Rename failed: {0} to {1}, {2}", oldname, newname, serverEx.GetInfo()); throw; } }