Пример #1
0
 /// 创建存储桶并且授予存储桶权限
 public void PutBucketAndGrantAcl()
 {
     //.cssg-snippet-body-start:[put-bucket-and-grant-acl]
     try
     {
         string           bucket  = "examplebucket-1250000000"; //格式:BucketName-APPID
         PutBucketRequest request = new PutBucketRequest(bucket);
         // 设置为共有读
         request.SetCosACL(CosACL.PUBLIC_READ);
         //授予1131975903账号写权限
         COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount();
         readAccount.AddGrantAccount("1131975903", "1131975903");
         request.SetXCosGrantWrite(readAccount);
         //执行请求
         PutBucketResult result = cosXml.PutBucket(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
 }
Пример #2
0
        /// 创建存储桶
        public void PutBucket()
        {
            //.cssg-snippet-body-start:[put-bucket]
            try
            {
                string           bucket  = "examplebucket-1250000000"; //格式:BucketName-APPID
                PutBucketRequest request = new PutBucketRequest(bucket);
                //执行请求
                PutBucketResult result = cosXml.PutBucket(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
        }
Пример #3
0
        public static void AsynPutBucket(COSXML.CosXml cosXml, string bucket)
        {
            PutBucketRequest request = new PutBucketRequest(bucket);

            //设置签名有效时长
            request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

            ///执行请求
            cosXml.PutBucket(request,
                             delegate(CosResult cosResult)
            {
                PutBucketResult result = cosResult as PutBucketResult;
                Console.WriteLine(result.GetResultInfo());
                Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId));
            },
                             delegate(CosClientException clientEx, CosServerException serverEx)
            {
                if (clientEx != null)
                {
                    QLog.D("XIAO", clientEx.Message);
                    Console.WriteLine("CosClientException: " + clientEx.StackTrace);
                }
                if (serverEx != null)
                {
                    QLog.D("XIAO", serverEx.Message);
                    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                }
                Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId));
            });
        }
Пример #4
0
        public async Task <ResponseModel> CreateBucket(string buketName)
        {
            try
            {
                string           bucket  = buketName + "-" + _appid; //存储桶名称 格式:BucketName-APPID
                PutBucketRequest request = new PutBucketRequest(bucket);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
                //执行请求
                PutBucketResult result = await Task.FromResult(_cosXml.PutBucket(request));

                return(new ResponseModel {
                    Code = 200, Message = result.GetResultInfo()
                });
            }
            catch (CosClientException clientEx)
            {
                return(new ResponseModel {
                    Code = 0, Message = "CosClientException: " + clientEx.Message + clientEx.InnerException
                });
            }
            catch (CosServerException serverEx)
            {
                return(new ResponseModel {
                    Code = 200, Message = "CosServerException: " + serverEx.GetInfo()
                });
            }
        }
Пример #5
0
        public void putBucket()
        {
            //.cssg-snippet-body-start:[put-bucket]
            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
                PutBucketRequest request = new PutBucketRequest(bucket);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
                //执行请求
                PutBucketResult result = cosXml.PutBucket(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
        }
Пример #6
0
        internal static void PutBucket(CosXmlServer cosXml)
        {
            try
            {
                PutBucketRequest request = new PutBucketRequest(bucket);

                //执行请求
                PutBucketResult result = cosXml.PutBucket(request);

                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                if (serverEx.statusCode != 409)
                {
                    throw serverEx;
                }
                else
                {
                    Console.WriteLine("Bucket Already exists.");
                }
            }
        }
Пример #7
0
        public static void PutBucket(COSXML.CosXml cosXml, string bucket)
        {
            try
            {
                PutBucketRequest request = new PutBucketRequest(bucket);
                //设置签名有效时长
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
                //执行请求
                PutBucketResult result = cosXml.PutBucket(request);

                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                QLog.D("XIAO", clientEx.Message);
                Console.WriteLine("CosClientException: " + clientEx.Message);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                QLog.D("XIAO", serverEx.Message);
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
        }
Пример #8
0
 public void CreateBucke(string name)
 {
     try
     {
         // string bucket = "examplebucket-1250000000"; //存储桶名称 格式:BucketName-APPID
         PutBucketRequest request = new PutBucketRequest(name);
         //设置签名有效时长
         request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
         //执行请求
         PutBucketResult result = cosSserver.PutBucket(request);
         //请求成功
         Console.WriteLine(result.GetResultInfo());
     }
     catch (COSXML.CosException.CosClientException clientEx)
     {
         //请求失败
         Console.WriteLine("CosClientException: " + clientEx.Message);
     }
     catch (COSXML.CosException.CosServerException serverEx)
     {
         //请求失败
         Console.WriteLine("CosServerException: " + serverEx.GetInfo());
     }
 }