Example #1
0
        /// <summary>
        ///   Construct a new HttpWebRequest for AWS, for the specified resource,
        ///   within the specified bucket, and using the given headers.
        /// </summary>
        /// <remarks>
        ///   <para>
        ///     The constructed request includes the Authorization header,
        ///     containing the HMACSHA1 signature required by Amaazon AWS.
        ///   </para>
        ///   <para>
        ///     This method does not actually *send* the request to AWS.  It
        ///     merely constructs it.  The caller is responsible for sending.
        ///     Calling request.GetResponse() will do so implicitly.
        ///   </para>
        ///   <para>
        ///     If there is an S3Object specified, as for example when uploading a
        ///     file to S3, only the metadata associated to that object is added
        ///     to the constructed request.  The caller must explicitly send the
        ///     data associated to the S3Object stream, before calling
        ///     GetResponse().
        ///   </para>
        /// </remarks>
        /// <param name="method">The HTTP method to use (GET, PUT, DELETE)</param>
        /// <param name="bucket">The bucket name</param>
        /// <param name="resource">The resource name (key)</param>
        /// <param name="headers">A map of string to string representing the HTTP headers to pass (can be null)</param>
        /// <param name="obj">S3Object that is to be written (can be null).</param>
        private HttpWebRequest MakeRequest(string method,
                                           string bucket,
                                           string resource,
                                           Dictionary <String, String> headers,
                                           S3Object obj)
        {
            string url = MakeURL(bucket, resource);
            var    req = WebRequest.Create(url) as HttpWebRequest;

            req.Timeout = System.Threading.Timeout.Infinite;

            if (req is HttpWebRequest)
            {
                (req as HttpWebRequest).AllowWriteStreamBuffering = false;
            }

            req.Method = method;
            AddHeaders(req, headers);
            if (obj != null)
            {
                AddMetadataHeaders(req, obj.Metadata);
            }
            AddAuthHeader(req, bucket, resource);

            return(req);
        }
Example #2
0
        private void UploadImageStream(Stream imageStream, string imageName)
        {
            if (!VerifyBasicSettings())
            {
                return;
            }

            Service  s3  = new Service(_settings.AccessKeyId, _settings.SecretAccessKey);
            S3Object obj = new S3Object
            {
                Stream = imageStream
            };
            var headers = new Dictionary <String, String>();

            headers.Add("x-amz-acl", "public-read");
            headers.Add("Content-Type", "image/png");
            var r = s3.Put(_settings.BucketName, imageName, obj, headers);

            if (r.StatusCode != System.Net.HttpStatusCode.OK)
            {
                MessageBox.Show("Status: " + r.StatusCode + ": " + r.GetResponseMessage());
            }
            else
            {
                string url = string.Format("http://s3.amazonaws.com/{0}/{1}",
                                           _settings.BucketName, imageName);
                Clipboard.SetText(url, TextDataFormat.Text);
            }
        }
Example #3
0
        private HttpWebRequest MakeRequest(string method,
                                           string resource,
                                           Dictionary <String, String> headers,
                                           S3Object obj)

        {
            return(MakeRequest(method, null, resource, headers, obj));
        }
Example #4
0
        /// <summary>
        /// Creates a new bucket.
        /// </summary>
        /// <param name="bucket">The name of the bucket to create</param>
        /// <param name="headers">A Map of string to string representing the headers to pass (can be null)</param>
        public WebResponse CreateBucket(string bucket, Dictionary <String, String> headers)
        {
            S3Object   obj     = new S3Object();
            WebRequest request = MakeRequest("PUT", bucket, headers, obj);

            request.ContentLength = 0;
            request.GetRequestStream().Close();
            return(request.GetResponse());
        }
Example #5
0
        /// <summary>
        /// Writes an object to S3.
        /// </summary>
        /// <param name="bucket">The name of the bucket to which the object will be added.</param>
        /// <param name="key">The name of the key to use</param>
        /// <param name="obj">An S3Object containing the data to write.</param>
        /// <param name="headers">A map of string to string representing the HTTP headers to pass (can be null)</param>
        public HttpWebResponse Put(string bucket, string key, S3Object obj, Dictionary <String, String> headers)
        {
            var request = MakeRequest("PUT",
                                      bucket,
                                      EncodeKeyForSignature(key),
                                      headers,
                                      obj);

            request.ContentLength = obj.Stream.Length;
            write(key, obj.Stream, request.GetRequestStream());
            obj.Stream.Close();
            request.GetRequestStream().Close();

            return(request.GetResponse() as HttpWebResponse);
        }
Example #6
0
        /// <summary>
        /// Write a new logging xml document for a given bucket
        /// </summary>
        /// <param name="bucket">The name of the bucket to enable/disable logging on</param>
        /// <param name="loggingXMLDoc">The xml representation of the logging configuration as a String.</param>
        /// <param name="headers">A map of string to string representing the HTTP headers to pass (can be null)</param>
        public HttpWebResponse PutBucketLogging(string bucket,
                                                string loggingXMLDoc,
                                                Dictionary <String, String> headers)
        {
            S3Object obj = new S3Object
            {
                Stream = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(loggingXMLDoc))
            };

            WebRequest request = MakeRequest("PUT", bucket + "?logging", headers, obj);

            request.ContentLength = obj.Stream.Length;
            write(obj.Stream, request.GetRequestStream());
            obj.Stream.Close();
            request.GetRequestStream().Close();

            return(request.GetResponse() as HttpWebResponse);
        }
Example #7
0
        /// <summary>
        /// Write a new ACL for a given object
        /// </summary>
        /// <param name="bucket">The name of the bucket where the object lives or the
        /// name of the bucket to change the ACL if key is null.</param>
        /// <param name="key">The name of the key to use; can be null.</param>
        /// <param name="aclXMLDoc">An XML representation of the ACL as a string.</param>
        /// <param name="headers">A map of string to string representing the HTTP headers to pass (can be null)</param>
        public HttpWebResponse PutACL(string bucket,
                                      string key,
                                      string aclXMLDoc,
                                      Dictionary <String, String> headers)
        {
            S3Object obj = new S3Object
            {
                Stream = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(aclXMLDoc))
            };

            if (key == null)
            {
                key = "";
            }

            var request = MakeRequest("PUT", bucket + "/" + EncodeKeyForSignature(key) + "?acl", headers, obj);

            request.ContentLength = obj.Stream.Length;
            write(obj.Stream, request.GetRequestStream());
            obj.Stream.Close();
            request.GetRequestStream().Close();

            return(request.GetResponse() as HttpWebResponse);
        }