/// 设置对象 ACL public void PutObjectAcl() { //.cssg-snippet-body-start:[put-object-acl] // 因为存储桶 ACL 最多1000条,为避免 ACL 达到上限, // 非必须情况不建议给对象单独设置 ACL(对象默认继承 bucket 权限). try { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 PutObjectACLRequest request = new PutObjectACLRequest(bucket, key); //设置私有读写权限 request.SetCosACL(CosACL.Private); //授予1131975903账号读权限 COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); //执行请求 PutObjectACLResult result = cosXml.PutObjectACL(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 }
/// 设置存储桶 ACL public void PutBucketAcl() { //.cssg-snippet-body-start:[put-bucket-acl] try { string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID PutBucketACLRequest request = new PutBucketACLRequest(bucket); //设置私有读写权限 request.SetCosACL(CosACL.PRIVATE); //授予1131975903账号读权限 COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); //执行请求 PutBucketACLResult result = cosXml.PutBucketACL(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 PutObjectACL() { try { PutObjectACLRequest request = new PutObjectACLRequest(bucket, commonKey); //添加acl request.SetCosACL(CosACL.PRIVATE); COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); COSXML.Model.Tag.GrantAccount fullAccount = new COSXML.Model.Tag.GrantAccount(); fullAccount.AddGrantAccount("2832742109", "2832742109"); request.SetXCosReadWrite(fullAccount); //执行请求 PutObjectACLResult result = cosXml.PutObjectACL(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.StackTrace); Assert.True(false); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.True(false); } }
public void putBucketAcl() { //.cssg-snippet-body-start:[put-bucket-acl] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "bucket-cssg-test-dotnet-1253653367"; //格式:BucketName-APPID PutBucketACLRequest request = new PutBucketACLRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置私有读写权限 request.SetCosACL(CosACL.PRIVATE); //授予1131975903账号读权限 COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); //执行请求 PutBucketACLResult result = cosXml.PutBucketACL(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }
public void AsynPutObject(COSXML.CosXml cosXml, string bucket, string key, string srcPath) { PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置进度回调 request.SetCosProgressCallback(delegate(long completed, long total) { //Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total)); }); //添加acl request.SetCosACL(CosACL.PRIVATE); COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); COSXML.Model.Tag.GrantAccount writeAccount = new COSXML.Model.Tag.GrantAccount(); writeAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantWrite(writeAccount); COSXML.Model.Tag.GrantAccount fullAccount = new COSXML.Model.Tag.GrantAccount(); fullAccount.AddGrantAccount("2832742109", "2832742109"); request.SetXCosReadWrite(fullAccount); cosXml.PutObject(request, delegate(CosResult cosResult) { PutObjectResult result = cosResult as PutObjectResult; Console.WriteLine(result.GetResultInfo()); manualResetEvent.Set(); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { Console.WriteLine("CosClientException: " + clientEx.Message); } if (serverEx != null) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } manualResetEvent.Set(); }); }
public void AsynPutObjectACL(COSXML.CosXml cosXml, string bucket, string key) { PutObjectACLRequest request = new PutObjectACLRequest(bucket, key); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //添加acl request.SetCosACL(CosACL.PRIVATE); COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); COSXML.Model.Tag.GrantAccount writeAccount = new COSXML.Model.Tag.GrantAccount(); writeAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantWrite(writeAccount); COSXML.Model.Tag.GrantAccount fullAccount = new COSXML.Model.Tag.GrantAccount(); fullAccount.AddGrantAccount("2832742109", "2832742109"); request.SetXCosReadWrite(fullAccount); cosXml.PutObjectACL(request, delegate(CosResult cosResult) { PutObjectACLResult result = cosResult as PutObjectACLResult; Console.WriteLine(result.GetResultInfo()); manualResetEvent.Set(); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { Console.WriteLine("CosClientException: " + clientEx.Message); } if (serverEx != null) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } manualResetEvent.Set(); }); }
public void AsynPutObject() { AutoResetEvent resetEvent = new AutoResetEvent(false); PutObjectRequest request = new PutObjectRequest(bucket, commonKey, smallFileSrcPath); //添加acl request.SetCosACL(CosACL.PRIVATE); COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); COSXML.Model.Tag.GrantAccount writeAccount = new COSXML.Model.Tag.GrantAccount(); writeAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantWrite(writeAccount); COSXML.Model.Tag.GrantAccount fullAccount = new COSXML.Model.Tag.GrantAccount(); fullAccount.AddGrantAccount("2832742109", "2832742109"); request.SetXCosReadWrite(fullAccount); cosXml.PutObject(request, delegate(CosResult cosResult) { PutObjectResult result = cosResult as PutObjectResult; Console.WriteLine(result.GetResultInfo()); resetEvent.Set(); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { Console.WriteLine("CosClientException: " + clientEx.Message); } if (serverEx != null) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } resetEvent.Set(); }); resetEvent.WaitOne(); }
public void PutObjectACL(COSXML.CosXml cosXml, string bucket, string key) { try { PutObjectACLRequest request = new PutObjectACLRequest(bucket, key); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //添加acl request.SetCosACL(CosACL.PRIVATE); COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount(); readAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantRead(readAccount); COSXML.Model.Tag.GrantAccount writeAccount = new COSXML.Model.Tag.GrantAccount(); writeAccount.AddGrantAccount("1131975903", "1131975903"); request.SetXCosGrantWrite(writeAccount); COSXML.Model.Tag.GrantAccount fullAccount = new COSXML.Model.Tag.GrantAccount(); fullAccount.AddGrantAccount("2832742109", "2832742109"); request.SetXCosReadWrite(fullAccount); //执行请求 PutObjectACLResult result = cosXml.PutObjectACL(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.StackTrace); Assert.True(false); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.True(false); } }