public void PutObjectWithPreSignedUriWithParameterNegativeTest()
        {
            var targetObject = OssTestUtils.GetObjectKey(_className);
            var gpuRequest   = new GeneratePresignedUriRequest(_bucketName, targetObject, SignHttpMethod.Put)
            {
                ContentType = "text/rtf",
                Expiration  = DateTime.Now.AddHours(1),
            };

            gpuRequest.UserMetadata.Add("Author", "oss");
            gpuRequest.UserMetadata.Add("Test", "test");
            gpuRequest.AddQueryParam("x-param-null", "");
            gpuRequest.AddQueryParam("x-param-space0", " ");
            gpuRequest.AddQueryParam("x-param-value", "value");
            gpuRequest.AddQueryParam("x-param-space1", " ");

            var uri = _ossClient.GeneratePresignedUri(gpuRequest);

            var metadata = new ObjectMetadata();

            metadata.ContentType = "text/rtf";
            metadata.UserMetadata.Add("Author", "oss");
            metadata.UserMetadata.Add("Test", "test1");
            try
            {
                var putObjectResult = _ossClient.PutObject(uri, Config.UploadTestFile, metadata);
                Assert.AreEqual(putObjectResult.HttpStatusCode, HttpStatusCode.Forbidden);
            }
            catch (OssException e)
            {
                Assert.AreEqual(OssErrorCode.SignatureDoesNotMatch, e.ErrorCode);
            }
        }
Example #2
0
        //Object
        public string GetObjectSignedUrl(string bucketName, string key, Dictionary <string, string> queryParams, Dictionary <string, string> userMataData)
        {
            var policyConds = new PolicyConditions();
            var client      = this.Client;
            var metadata    = client.GetObjectMetadata(bucketName, key);
            var etag        = metadata.ETag;
            var req         = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);

            // Set optional properties(be blind to them usually)
            if (queryParams != null)
            {
                foreach (var item in queryParams)
                {
                    req.AddQueryParam(item.Key, item.Value);
                }
            }
            if (userMataData != null)
            {
                foreach (var item in userMataData)
                {
                    req.AddQueryParam(item.Key, item.Value);
                }
            }

            req.ContentType = "text/html";
            req.ContentMd5  = etag;
            req.ResponseHeaders.CacheControl    = "No-Cache";
            req.ResponseHeaders.ContentEncoding = "utf-8";
            req.ResponseHeaders.ContentType     = "text/html";
            var uri = client.GeneratePresignedUri(req);

            return(uri.ToString());
        }
        public static void GetObjectBySignedUrlWithWebRequest(string bucketName, string key)
        {
            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                var etag     = metadata.ETag;

                var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);
                req.AddQueryParam("param1", "value1");
                req.ContentType = "text/html";
                req.ContentMd5  = etag;
                req.AddUserMetadata("mk1", "mv1");
                req.ResponseHeaders.CacheControl    = "No-Cache";
                req.ResponseHeaders.ContentEncoding = "utf-8";
                req.ResponseHeaders.ContentType     = "text/html";

                // Generates url signature for accessing specified object.
                var uri = client.GeneratePresignedUri(req);

                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.ContentType = "text/html";
                webRequest.Headers.Add(HttpRequestHeader.ContentMd5, etag);
                webRequest.Headers.Add("x-oss-meta-mk1", "mv1");
                var resp       = webRequest.GetResponse() as HttpWebResponse;
                var output     = resp.GetResponseStream();
                var bufferSize = 2048;
                var bytes      = new byte[bufferSize];
                try
                {
                    using (StreamWriter outfile = new StreamWriter(fileToSave))
                    {
                        var length = 0;
                        do
                        {
                            length = output.Read(bytes, 0, bufferSize);
                            outfile.Write(bytes);
                        } while (length > 0);
                    }
                    output.Close();

                    Console.WriteLine("Get object by signatrue succeeded.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ex : " + ex.Message);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        public static void GetObjectBySignedUrlWithWebRequest(string bucketName, string key)
        {
            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                var etag = metadata.ETag;

                var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);
                req.AddQueryParam("param1", "value1");
                req.ContentType = "text/html";
                req.ContentMd5 = etag;
                req.AddUserMetadata("mk1", "mv1");
                req.ResponseHeaders.CacheControl = "No-Cache";
                req.ResponseHeaders.ContentEncoding = "utf-8";
                req.ResponseHeaders.ContentType = "text/html";

                // Generates url signature for accessing specified object.
                var uri = client.GeneratePresignedUri(req);

                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.ContentType = "text/html";
                webRequest.Headers.Add(HttpRequestHeader.ContentMd5, etag);
                webRequest.Headers.Add("x-oss-meta-mk1", "mv1");
                var resp = webRequest.GetResponse() as HttpWebResponse;
                var output = resp.GetResponseStream();
                var bufferSize = 2048;
                var bytes = new byte[bufferSize];
                try
                {
                    using (StreamWriter outfile = new StreamWriter(fileToSave)) 
                    {
                        var length = 0;
                        do
                        {
                            length = output.Read(bytes, 0, bufferSize);
                            outfile.Write(bytes);
                        } while (length > 0);
                    }
                    output.Close();

                    Console.WriteLine("Get object by signatrue succeeded.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ex : " + ex.Message);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", 
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
Example #5
0
        public void GeneratePresignedUriCallbackTestWithParameter()
        {
            string callbackHeaderBuilder         = new CallbackHeaderBuilder(_callbackUrl, _callbackBody).Build();
            string CallbackVariableHeaderBuilder = new CallbackVariableHeaderBuilder().
                                                   AddCallbackVariable("x:var1", "x:value1").AddCallbackVariable("x:var2", "x:value2").Build();

            var gpuRequest = new GeneratePresignedUriRequest(_bucketName, _objectKey, SignHttpMethod.Put)
            {
                ContentType = "text/rtf",
                Expiration  = DateTime.Now.AddHours(1),
                Callback    = callbackHeaderBuilder,
                CallbackVar = CallbackVariableHeaderBuilder
            };

            gpuRequest.UserMetadata.Add("Author", "Mingdi");
            gpuRequest.AddQueryParam("x-param-null", "");
            gpuRequest.AddQueryParam("x-param-space0", " ");
            gpuRequest.AddQueryParam("x-param-value", "value");
            gpuRequest.AddQueryParam("x-param-space1", " ");
            var uri = _ossClient.GeneratePresignedUri(gpuRequest);

            var metadata = new ObjectMetadata();

            metadata.ContentType = "text/rtf";
            metadata.UserMetadata.Add("Author", "Mingdi");
            metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder);
            metadata.AddHeader(HttpHeaders.CallbackVar, CallbackVariableHeaderBuilder);

            var putObjectResult = _ossClient.PutObject(uri, Config.UploadTestFile, metadata);

            Assert.IsTrue(putObjectResult.IsSetResponseStream());
            Assert.AreEqual(_callbackOkResponse, GetCallbackResponse(putObjectResult));
            Assert.AreEqual(putObjectResult.HttpStatusCode, HttpStatusCode.OK);
            Assert.AreEqual(putObjectResult.ContentLength, _callbackOkResponse.Length);
            Assert.AreEqual(putObjectResult.RequestId.Length, "58DB0ACB686D42D5B4163D75".Length);
            Assert.AreEqual(putObjectResult.ResponseMetadata[HttpHeaders.ContentType], "application/json");
            Assert.AreEqual(putObjectResult.ResponseMetadata[HttpHeaders.ETag], putObjectResult.ETag);
            Assert.AreEqual(putObjectResult.ResponseMetadata[HttpHeaders.ContentMd5].Length, "7GoU4OYaYroKXsbsG1f/lw==".Length);
            Assert.IsTrue(putObjectResult.ResponseMetadata[HttpHeaders.HashCrc64Ecma].Length > 0);
            Assert.IsTrue(putObjectResult.ResponseMetadata[HttpHeaders.ServerElapsedTime].Length > 0);
            Assert.AreEqual(putObjectResult.ResponseMetadata[HttpHeaders.Date].Length, "Wed, 29 Mar 2017 01:15:58 GMT".Length);

            _ossClient.DeleteObject(_bucketName, _objectKey);
        }
        public void GetObjectWithPreSignedAndAclParameter()
        {
            var targetObject = OssTestUtils.GetObjectKey(_className);

            OssTestUtils.UploadObject(_ossClient, _bucketName, targetObject, Config.UploadTestFile);
            var gpuRequest = new GeneratePresignedUriRequest(_bucketName, targetObject, SignHttpMethod.Get)
            {
                Expiration = DateTime.Now.AddHours(1),
            };

            gpuRequest.AddQueryParam("acl", "");
            var uri = _ossClient.GeneratePresignedUri(gpuRequest);

            try
            {
                var getObjectResult = _ossClient.GetObject(uri);
                Assert.AreEqual(getObjectResult.HttpStatusCode, HttpStatusCode.OK);

                var xml   = new XmlSerializer(typeof(AccessControlPolicy));
                var model = xml.Deserialize(getObjectResult.Content) as AccessControlPolicy;
                var acl   = new AccessControlList();
                foreach (var grant in model.Grants)
                {
                    if (grant == EnumUtils.GetStringValue(CannedAccessControlList.PublicRead))
                    {
                        acl.GrantPermission(GroupGrantee.AllUsers, Permission.Read);
                        acl.ACL = CannedAccessControlList.PublicRead;
                    }
                    else if (grant == EnumUtils.GetStringValue(CannedAccessControlList.PublicReadWrite))
                    {
                        acl.GrantPermission(GroupGrantee.AllUsers, Permission.FullControl);
                        acl.ACL = CannedAccessControlList.PublicReadWrite;
                    }
                    else if (grant == EnumUtils.GetStringValue(CannedAccessControlList.Private))
                    {
                        acl.ACL = CannedAccessControlList.Private;
                    }
                    else if (grant == EnumUtils.GetStringValue(CannedAccessControlList.Default))
                    {
                        acl.ACL = CannedAccessControlList.Default;
                    }
                }

                var acl1 = _ossClient.GetObjectAcl(_bucketName, targetObject);
                Assert.AreEqual(acl1.ACL, acl.ACL);
            }
            catch (OssException e)
            {
                Assert.Fail(e.Message);
            }
        }
 public void GeneratePresignedUriWithExecptionTest()
 {
     try
     {
         var targetObject = OssTestUtils.GetObjectKey(_className);
         var gpuRequest   = new GeneratePresignedUriRequest(_bucketName, targetObject, SignHttpMethod.Head)
         {
             ContentType = "text/rtf",
             Expiration  = DateTime.Now.AddHours(1),
         };
         gpuRequest.UserMetadata.Add("Author", "oss");
         gpuRequest.UserMetadata.Add("Test", "test");
         gpuRequest.AddQueryParam("x-param-null", "");
         gpuRequest.AddQueryParam("x-param-space0", " ");
         gpuRequest.AddQueryParam("x-param-value", "value");
         gpuRequest.AddQueryParam("x-param-space1", " ");
         var uri = _ossClient.GeneratePresignedUri(gpuRequest);
         Assert.IsTrue(false);
     }
     catch
     {
         Assert.IsTrue(true);
     }
 }
Example #8
0
        public void SignedUrlTest()
        {
            var key     = OssTestUtils.GetObjectKey(_className);
            var content = new MemoryStream(Encoding.ASCII.GetBytes(_data_200KB));

            _ossClient.PutObject(new PutObjectRequest(_bucketName, key, content));

            var request = new GeneratePresignedUriRequest(_bucketName, key, SignHttpMethod.Get);

            request.AddQueryParam("x-oss-traffic-limit", "819200");
            var uri = _ossClient.GeneratePresignedUri(request);

            Assert.AreNotEqual(uri.ToString().IndexOf("x-oss-traffic-limit=819200"), -1);
            var result = _ossClient.GetObject(uri);

            Assert.AreEqual(result.ResponseMetadata.ContainsKey(HttpHeaders.QosDelayTime), true);
        }
        public static void getObjectBySignedUrl()
        {
            const string accessKeyId = "<your access key id>";
            const string accessKeySecret = "<your access key secret>";
            const string endpoint = "<valid host name>";

            const string bucketName = "<bucket name>";
            const string key = "<object name>";

            OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);

            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                var etag = metadata.ETag;

                var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);
                // Set optional properties(be blind to them usually)
                req.AddQueryParam("param1", "value1");
                req.ContentType = "text/html";
                req.ContentMd5 = etag;
                req.AddUserMetadata("mk1", "mv1");
                req.AddUserMetadata("mk2", "mv2");
                req.ResponseHeaders.CacheControl = "No-Cache";
                req.ResponseHeaders.ContentEncoding = "utf-8";
                req.ResponseHeaders.ContentType = "text/html";
                // Generates url signature for accessing specified object.
                var uri = client.GeneratePresignedUri(req);

                Console.WriteLine(uri.ToString());

                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.ContentType = "text/html";
                webRequest.Headers.Add(HttpRequestHeader.ContentMd5, etag);
                webRequest.Headers.Add("x-oss-meta-mk1", "mv1");
                webRequest.Headers.Add("x-oss-meta-mk2", "mv2");
                var resp = webRequest.GetResponse() as HttpWebResponse;
                var output = resp.GetResponseStream();
                var bufferSize = 2048;
                var bytes = new byte[bufferSize];
                try
                {
                    var length = 0;
                    do
                    {
                        length = output.Read(bytes, 0, bufferSize);
                        // to do something with bytes...
                    } while (length > 0);
                    output.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ex : " + ex.Message);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
Example #10
0
        public static void getObjectBySignedUrl()
        {
            const string accessKeyId     = "<your access key id>";
            const string accessKeySecret = "<your access key secret>";
            const string endpoint        = "<valid host name>";

            const string bucketName = "<bucket name>";
            const string key        = "<object name>";

            OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);

            try
            {
                var metadata = client.GetObjectMetadata(bucketName, key);
                var etag     = metadata.ETag;

                var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);
                // Set optional properties(be blind to them usually)
                req.AddQueryParam("param1", "value1");
                req.ContentType = "text/html";
                req.ContentMd5  = etag;
                req.AddUserMetadata("mk1", "mv1");
                req.AddUserMetadata("mk2", "mv2");
                req.ResponseHeaders.CacheControl    = "No-Cache";
                req.ResponseHeaders.ContentEncoding = "utf-8";
                req.ResponseHeaders.ContentType     = "text/html";
                // Generates url signature for accessing specified object.
                var uri = client.GeneratePresignedUri(req);

                Console.WriteLine(uri.ToString());

                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.ContentType = "text/html";
                webRequest.Headers.Add(HttpRequestHeader.ContentMd5, etag);
                webRequest.Headers.Add("x-oss-meta-mk1", "mv1");
                webRequest.Headers.Add("x-oss-meta-mk2", "mv2");
                var resp       = webRequest.GetResponse() as HttpWebResponse;
                var output     = resp.GetResponseStream();
                var bufferSize = 2048;
                var bytes      = new byte[bufferSize];
                try
                {
                    var length = 0;
                    do
                    {
                        length = output.Read(bytes, 0, bufferSize);
                        // to do something with bytes...
                    } while (length > 0);
                    output.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ex : " + ex.Message);
                }
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }