public void GetPreSignedUriFullSettingsPositiveTest()
        {
            HttpWebRequest  req = null;
            HttpWebResponse res = null;

            var now = DateTime.Now;
            //set expiration time to 5 seconds later
            var expireDate = now.AddSeconds(5);
            var gpuRequest = new GeneratePresignedUriRequest(_bucketName, _objectKey)
            {
                Expiration      = expireDate,
                ContentType     = "application/zip",
                ContentMd5      = _objectETag,
                ResponseHeaders =
                {
                    CacheControl       = "No-cache",
                    ContentType        = "application/zip",
                    ContentDisposition = "myDownload.zip"
                }
            };

            gpuRequest.AddUserMetadata("name1", "vaue1");
            gpuRequest.AddUserMetadata("name2", "vaue2");
            var uri = _ossClient.GeneratePresignedUri(gpuRequest);

            try
            {
                req        = WebRequest.Create(uri) as HttpWebRequest;
                req.Method = "GET";

                req.ContentType = "application/zip";
                req.Headers.Add(HttpRequestHeader.ContentMd5, _objectETag);

                req.Headers.Add("x-oss-meta-name1", "vaue1");
                req.Headers.Add("x-oss-meta-name2", "vaue2");
                res = req.GetResponse() as HttpWebResponse;
                Assert.AreEqual(HttpStatusCode.OK, res.StatusCode, "response status code is not expected.");
                using (var stream = res.GetResponseStream())
                {
                    var actualETag = FileUtils.ComputeContentMd5(stream);
                    Assert.AreEqual(_objectETag.ToLowerInvariant(), actualETag.ToLowerInvariant());
                }
            }
            catch (WebException)
            {
                //TODO:
            }
            finally
            {
                if (req != null)
                {
                    req.Abort();
                }
                if (res != null)
                {
                    res.Close();
                }
            }
        }
        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);
            }
        }
        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);
            }
        }
예제 #5
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);
            }
        }