示例#1
0
        public GetResponse(WebResponse response)
            : base(response)
        {
            var metadata = extractMetadata(response);
            var data     = Utils.slurpInputStream(response.GetResponseStream());

            obj = new S3Object(data, metadata);
        }
        public string put(string bucket, string key, S3Object obj, SortedList headers)
        {
            SortedList metadata = null;

            if (obj != null)
            {
                metadata = obj.Metadata;
            }

            return(generateURL("PUT", bucket, key, mergeMeta(headers, metadata)));
        }
示例#3
0
        static int Main(string[] args)
        {
            try {
                if (awsAccessKeyId.StartsWith("<INSERT"))
                {
                    Console.WriteLine("Please examine S3Driver.cs and update it with your credentials");
                    return(1);
                }

                var conn      = new AWSAuthConnection(awsAccessKeyId, awsSecretAccessKey);
                var generator = new QueryStringAuthGenerator(awsAccessKeyId, awsSecretAccessKey);

                // Check if the bucket exists.  The high availability engineering of
                // Amazon S3 is focused on get, put, list, and delete operations.
                // Because bucket operations work against a centralized, global
                // resource space, it is not appropriate to make bucket create or
                // delete calls on the high availability code path of your application.
                // It is better to create or delete buckets in a separate initialization
                // or setup routine that you run less often.
                if (conn.checkBucketExists(bucketName))
                {
                    Console.WriteLine("----- bucket already exists! -----");
                }
                else
                {
                    Console.WriteLine("----- creating bucket -----");
                    // to create an EU located bucket change the Location param like this:
                    //  using ( Response response = conn.createBucket( bucketName, Location.EU, null ) )
                    using (var response = conn.createBucket(bucketName, Location.EU, null))
                        Console.WriteLine(response.getResponseMessage());
                }

                Console.WriteLine("----- bucket location -----");
                using (var locationResponse = conn.getBucketLocation(bucketName))
                    if (locationResponse.Location == null)
                    {
                        Console.WriteLine("Location: <error>");
                    }
                    else if (locationResponse.Location.Length == 0)
                    {
                        Console.WriteLine("Location: <default>");
                    }
                    else
                    {
                        Console.WriteLine("Location: '{0}'", locationResponse.Location);
                    }

                Console.WriteLine("----- listing bucket -----");
                using (var listBucketResponse = conn.listBucket(bucketName, null, null, 0, null)) dumpBucketListing(listBucketResponse);

                Console.WriteLine("----- putting object -----");
                var obj     = new S3Object("This is a test", null);
                var headers = new SortedList {
                    { "Content-Type", "text/plain" }
                };
                using (var response = conn.put(bucketName, keyName, obj, headers)) Console.WriteLine(response.getResponseMessage());

                Console.WriteLine("----- listing bucket -----");
                using (var listBucketResponse = conn.listBucket(bucketName, null, null, 0, null)) dumpBucketListing(listBucketResponse);

                Console.WriteLine("----- query string auth example -----");
                generator.ExpiresIn = 60 * 1000;

                Console.WriteLine("Try this url in your web browser (it will only work for 60 seconds)\n");
                var url = generator.get(bucketName, keyName, null);
                Console.WriteLine(url);
                Console.Write("\npress enter >");
                Console.ReadLine();

                Console.WriteLine(
                    "\nNow try just the url without the query string arguments.  It should fail.\n");
                Console.WriteLine(generator.makeBaseURL(bucketName, keyName));
                Console.Write("\npress enter >");
                Console.ReadLine();

                Console.WriteLine("----- putting object with metadata and public read acl -----");
                var metadata = new SortedList {
                    { "blah", "foo" }
                };
                obj = new S3Object("this is a publicly readable test", metadata);

                headers = new SortedList {
                    { "x-amz-acl", "public-read" }, { "Content-Type", "text/plain" }
                };
                using (var response = conn.put(bucketName, keyName + "-public", obj, headers)) Console.WriteLine(response.getResponseMessage());

                Console.WriteLine("----- anonymous read test -----");
                Console.WriteLine("\nYou should be able to try this in your browser\n");
                var publicURL = generator.get(bucketName, keyName + "-public", null);
                Console.WriteLine(publicURL);
                Console.Write("\npress enter >");
                Console.ReadLine();

                Console.WriteLine("----- path style url example -----");
                Console.WriteLine(
                    "\nNon-location-constrained buckets can also be specified as part of the url path.  (This was the original url style supported by S3.)");
                Console.WriteLine("\nTry this url out in your browser (it will only be valid for 60 seconds)\n");
                generator.CallingFormat = CallingFormat.PATH;
                // could also have been done like this:
                //  generator = new QueryStringAuthGenerator(awsAccessKeyId, awsSecretAccessKey, true, Utils.DEFAULT_HOST, CallingFormat.getPathCallingFormat());
                generator.ExpiresIn = 60 * 1000;
                Console.WriteLine(generator.get(bucketName, keyName, null));
                Console.Write("\npress enter> ");
                Console.ReadLine();

                Console.WriteLine("----- getting object's acl -----");
                using (var response = conn.getACL(bucketName, keyName, null)) Console.WriteLine(response.Object.Data);

                Console.WriteLine("----- deleting objects -----");
                using (var response = conn.delete(bucketName, keyName, null)) Console.WriteLine(response.getResponseMessage());
                using (var response = conn.delete(bucketName, keyName + "-public", null)) Console.WriteLine(response.getResponseMessage());

                Console.WriteLine("----- listing bucket -----");
                using (var listBucketResponse = conn.listBucket(bucketName, null, null, 0, null)) dumpBucketListing(listBucketResponse);

                Console.WriteLine("----- listing all my buckets -----");
                using (var listBucketResponse = conn.listAllMyBuckets(null)) dumpAllMyBucketListing(listBucketResponse);

                Console.WriteLine("----- deleting bucket -----");
                using (var response = conn.deleteBucket(bucketName, null)) Console.WriteLine(response.getResponseMessage());
                return(0);
            } catch (WebException e) {
                Console.WriteLine(e.Status);
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.ReadLine();
                return(1);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.ReadLine();
                return(1);
            }
        }
示例#4
0
        WebResponse makeRequest(
            string method, string bucket, string key, SortedList query, IDictionary headers, S3Object obj
            )
        {
            if (!String.IsNullOrEmpty(key))
            {
                key = Utils.urlEncode(key);
            }
            var url = buildUrl(bucket, key, query);

            var redirectCount = 0;

            for (;;)
            {
                // prep request
                var req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = method;
                // we handle redirects manually
                req.AllowAutoRedirect = false;
                // we already buffer everything
                req.AllowWriteStreamBuffering = false;
                // if we are sending >1kb data, ask for 100-Continue
                if (obj != null && obj.Bytes.Length > 1024)
                {
                    req.ServicePoint.Expect100Continue = true;
                }

                addHeaders(req, headers);
                if (obj != null)
                {
                    addMetadataHeaders(req, obj.Metadata);
                }
                addAuthHeader(req, bucket, key, query);

                if (obj != null)
                {
                    // Work around an HttpWebRequest bug where it will
                    // send the request body even if the server does *not*
                    // send 100 continue
                    req.KeepAlive = false;
                    // Write actual data
                    var data = obj.Bytes;
                    req.ContentLength = data.Length;
                    var requestStream = req.GetRequestStream();
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
                }

                // execute request
                HttpWebResponse response;
                try {
                    response = (HttpWebResponse)req.GetResponse();
                    if (!isRedirect(response))
                    {
                        return(response);
                    }
                    // retry against redirected url
                    url = response.Headers["Location"];
                    response.Close();
                    if (string.IsNullOrEmpty(url))
                    {
                        throw new WebException(
                                  "Redirect without Location header, may need to change calling format.",
                                  WebExceptionStatus.ProtocolError);
                    }
                } catch (WebException ex) {
                    if (ex.Response == null)
                    {
                        throw;
                    }
                    var msg = Utils.slurpInputStreamAsString(ex.Response.GetResponseStream());
                    throw new WebException(msg, ex, ex.Status, ex.Response);
                }

                redirectCount++;
                if (redirectCount > 10)
                {
                    throw new WebException("Too many redirects.");
                }
            }
        }
示例#5
0
 public Response put(string bucket, string key, S3Object obj, SortedList headers)
 {
     return(new Response(makeRequest("PUT", bucket, key, null, headers, obj)));
 }