/// <summary>
        /// 对一个请求计算前面并放入header中
        /// </summary>
        /// <param name="serviceRequest"></param>
        /// <param name="credential"></param>
        public static void SignRequest(ServiceRequest serviceRequest, OssCredential credential, HttpRequestMessage httpRequestMessage)
        {
            string httpMethod  = serviceRequest.HttpMethod.Method.ToUpperInvariant();
            string contentType = httpRequestMessage.Content?.Headers.ContentType?.ToString();
            string date        = serviceRequest.Headers[HttpHeaders.Date];
            string contentMd5  = serviceRequest.ContentMd5 == null ? "" : Convert.ToBase64String(serviceRequest.ContentMd5);
            string ossHeaders  = ComputeCanonicalizedOSSHeaders(serviceRequest.Headers, serviceRequest.Parameters);


            string resourcePath = BuildCanonicalizedResource(serviceRequest.Bucket, serviceRequest.ObjectKey, serviceRequest.Parameters);

            var sign = HmacSha1Sign(credential.AccessKeySecret, httpMethod, contentMd5, contentType, date, ossHeaders,
                                    resourcePath);

            httpRequestMessage.Headers.Add(HttpHeaders.Authorization, "OSS " + credential.AccessKeyId + ":" + sign);
        }
예제 #2
0
 public OssClient(OssCredential credential, ClientConfiguration config)
 {
     _requestContext = new RequestContext(credential, config);
 }
 public RequestContext(OssCredential credential, ClientConfiguration config)
 {
     OssCredential       = credential;
     ClientConfiguration = config;
 }
예제 #4
0
 public OssClient(OssCredential credential)
     : this(credential, ClientConfiguration.Default)
 {
 }
        internal static string Region = OssRegions.ShangHai; //设置要操作的区域



        public static async Task RunAsync()
        {
            var crediential = new OssCredential()
            {
                AccessKeyId     = AccessKeyId,
                AccessKeySecret = AssessSecret
            };

            var client = new OssClient(crediential);

            ////list buckets
            //var listBucketResult = await client.ListBucketsAsync(OssRegions.ShangHai);
            //Console.WriteLine(listBucketResult.IsSuccess + ":" + listBucketResult.ErrorMessage);

            var bucket = BucketInfo.CreateByRegion(Region, BucketName, false, false);

            // put string
            {
                string content   = "这是一个文本文件\naaaaaaaa\nbbbbbb\nccccccccc";
                var    putResult = await client.PutObjectAsync(bucket, "test_put_object_string.txt", content);

                Console.WriteLine($"Put string object  {putResult.IsSuccess} {putResult.ErrorMessage}  Etag:{putResult.SuccessResult?.ETag}");
            }



            // put file
            {
                var file      = @"D:\Work\Weixin\resource\IMG_1399.png";
                var putResult = await client.PutObjectByFileNameAsync(bucket, "test_put_file.png", file);

                Console.WriteLine($"Put file object  {putResult.IsSuccess} {putResult.ErrorMessage}  Etag:{putResult.SuccessResult?.ETag}");
            }

            //// copy file
            //Console.WriteLine("\n\n===Copy Object=============");
            //var copyResult =
            //    await
            //        client.CopyObjectAsync(bucket, "test_put_object_string.txt", bucket,
            //            "test_03_copy_object_string.txt", null);
            //Console.WriteLine($"Copy object  {copyResult.IsSuccess} {copyResult.ErrorMessage}  Etag:{copyResult.SuccessResult?.LastModified}");

            //// get file
            //Console.WriteLine("\n\n===Get Object=============");
            //var getResult = await client.GetObjectAsync(bucket, "test_put_object_string.txt");
            //Console.WriteLine($"Get Object = {getResult.IsSuccess}");
            //if (getResult.IsSuccess)
            //{
            //    var content = await getResult.SuccessResult.Content.ReadAsStringAsync();
            //    Console.WriteLine("FileContent" + content);
            //}

            //// append file
            //Console.WriteLine("\n\n===append Object=============");
            //{
            //    var content = "This is a line 这是一行字符串.";
            //    var file = new OssObjectInfo()
            //    {
            //        ContentType = RequestContentType.String,
            //        MimeType = "text/text",
            //        StringContent = content
            //    };
            //    var appendResult = await client.AppendObject(bucket, "test_append_object.txt", 0, file);
            //    Console.WriteLine($"1st append:{appendResult.IsSuccess} nextPos={appendResult.SuccessResult?.NextAppendPosition}");
            //    if (appendResult.IsSuccess)
            //    {
            //        appendResult = await client.AppendObject(bucket, "test_append_object.txt", appendResult.SuccessResult.NextAppendPosition, file);
            //        Console.WriteLine($"2st append:{appendResult.IsSuccess} nextPos={appendResult.SuccessResult?.NextAppendPosition}");

            //    }
            //}

            //// delete object
            //Console.WriteLine("\n\n===append Object=============");
            //{
            //    var content = "This is a line 这是一行字符串.";
            //    var key = "test_delete_object.txt";
            //    var putResult = await client.PutObjectAsync(bucket, key, content);
            //    if (putResult.IsSuccess)
            //    {
            //        var deleteResult = await client.DeleteObjectAsync(bucket, key);
            //        Console.WriteLine($"Delete reuslt:{deleteResult.IsSuccess} {deleteResult.ErrorMessage}");
            //    }
            //    else
            //    {
            //        Console.WriteLine($" Put object failed.{putResult.ErrorMessage}");
            //    }
            //}

            //// delete multiple
            //Console.WriteLine("\n\n===delete multiple=============");
            //{
            //    var content = "This is a line 这是一行字符串.";

            //    IList<string> fielKeys = new List<string>();

            //    for (int i = 1; i < 10; i++)
            //    {
            //        var key = $"test_delete_multi_object_{i}.txt";
            //        var putResult = await client.PutObjectAsync(bucket, key, content);
            //        if (putResult.IsSuccess == false)
            //        {
            //            Console.WriteLine($" Put object {i} failed.{putResult.ErrorMessage}");
            //            return;
            //        }
            //        fielKeys.Add(key);
            //    }

            //    var deleteResult = await client.DeleteMultipleObjectsAsync(bucket, fielKeys, false);

            //    Console.WriteLine($"Delete multi:{deleteResult.IsSuccess} {deleteResult.ErrorMessage}");

            //}


            // head object
            // delete multiple
            Console.WriteLine("\n\n===head object=============");
            {
                var content = "This is a line 这是一行字符串.";
                var key     = "test_head_object.txt";
                //var putResult = await client.PutObjectAsync(bucket, key, content);
                //if (putResult.IsSuccess)
                {
                    var headResult = await client.HeadObjectAsync(bucket, key, null);

                    Console.WriteLine($"Head object: {headResult.IsSuccess} {headResult.ErrorMessage}");
                }
            }


            Console.WriteLine("\n\n===Get object meta=============");
            {
                //var content = "This is a line 这是一行字符串.";
                var key = "test_get_meta_object.txt";
                //var putResult = await client.PutObjectAsync(bucket, key, content);
                //if (putResult.IsSuccess)
                {
                    var headResult = await client.GetObjectMetaAsync(bucket, key);

                    Console.WriteLine($"Head object: {headResult.IsSuccess} {headResult.ErrorMessage}");
                }
            }
        }