Example #1
0
        /// <summary>
        /// Writes an object to S3 using streaming.
        /// </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 Response putStream(string bucket, string key, S3StreamObject obj, SortedList headers)
        {
            Boolean isEmptyKey = (key == null) || (key.Length == 0)
                || (key.Trim().Length == 0);
            string pathSep = isEmptyKey ? "" : "/";

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

            /*WebRequest request = makeStreamRequest("PUT", bucket + pathSep
                + HttpUtility.UrlEncode(key), headers, obj);
            */

            WebRequest request = makeStreamRequest("PUT", bucket + pathSep
                + key, headers, obj);

            // cast WebRequest to a HttpWebRequest to allow for direct streaming of data
            HttpWebRequest hwr = (HttpWebRequest) request;
            hwr.AllowWriteStreamBuffering = false;
            hwr.SendChunked = false;
            hwr.ContentLength = obj.Stream.Length;

            ASCIIEncoding encoding = new ASCIIEncoding();

            byte[] buf = new byte[1024];
            BufferedStream bufferedInput = new BufferedStream(obj.Stream);
            int contentLength = 0;
            int bytesRead = 0;
            while ((bytesRead = bufferedInput.Read(buf, 0, 1024)) > 0)
            {
                contentLength += bytesRead;
                hwr.GetRequestStream().Write( buf, 0, bytesRead );
            }
            hwr.GetRequestStream().Close();

            return new Response( request );
        }
Example #2
0
        /// <summary>
        /// Make a new WebRequest
        /// </summary>
        /// <param name="method">The HTTP method to use (GET, PUT, DELETE)</param>
        /// <param name="resource">The resource name (bucket + "/" + key)</param>
        /// <param name="headers">A map of string to string representing the HTTP headers to pass (can be null)</param>
        /// <param name="obj">S3StreamObject that is to be written (can be null).</param>
        private WebRequest makeStreamRequest( string method, string resource, SortedList headers, S3StreamObject obj )
        {
            string url = makeURL( resource );
            WebRequest req = WebRequest.Create( url );
            req.Timeout = 3600000; // 1 hr
            req.Method = method;

            addHeaders( req, headers );
            if ( obj != null ) addMetadataHeaders( req, obj.Metadata );
            addAuthHeader( req, resource );

            return req;
        }
Example #3
0
        public static Response PutStream(string bucket, string key, Stream stream, bool setPublic )
        {
            S3StreamObject s3Object = new S3StreamObject(stream, null);

            int lastSlash = Math.Max(key.LastIndexOf("/"), key.LastIndexOf("\\"));

            string fileName = key.Substring(lastSlash + 1);

            string extension = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();

            System.Collections.SortedList headers = new System.Collections.SortedList();

            bool setDisposition = false;

            if (extension == "exe" || extension == "bin")
            {
                headers["Content-Type"] = "application/octet-stream";
                setDisposition = true;
            }
            else if (extension == "zip")
            {
                headers["Content-Type"] = "application/zip";
                setDisposition = true;
            }
            else if (extension == "tar")
            {
                headers["Content-Type"] = "application/x-tar";
                setDisposition = true;
            }
            else if (extension == "gz")
            {
                headers["Content-Type"] = "application/x-gzip";
                setDisposition = true;
            }
            else if (extension == "png")
            {
                headers["Content-Type"] = "image/png";
            }
            else if (extension == "gif")
            {
                headers["Content-Type"] = "image/gif";
            }
            else if (extension == "jpg" || extension == "jpeg" )
            {
                headers["Content-Type"] = "image/jpeg";

            }

            if (setDisposition)
            {
                headers["Content-Disposition"] = "attachment; filename=\"" + fileName + "\"";
            }

            try
            {

                // Get a connection to s3
                AWSAuthConnection conn = AmazonS3.GetConnection();

                // Stream the file to S3
                Response response = conn.putStream(bucket, key, s3Object, headers);

                if (setPublic)
                {
                    string acl = "<AccessControlPolicy xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Owner><ID>673171425ac8d62a827ebfd26c1e51c0dd70016e822369e964467887ee3a0e7d</ID><DisplayName>amazon</DisplayName></Owner><AccessControlList><Grant><Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"Group\"><URI>http://acs.amazonaws.com/groups/global/AllUsers</URI></Grantee><Permission>READ</Permission></Grant><Grant><Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"CanonicalUser\"><ID>673171425ac8d62a827ebfd26c1e51c0dd70016e822369e964467887ee3a0e7d</ID></Grantee><Permission>FULL_CONTROL</Permission></Grant></AccessControlList></AccessControlPolicy>";
                    conn.putACL(bucket, key, acl, null);
                }

                return response;
            }
            catch
            {
                // Probably couldn't connect ro s3
                return null;
            }
        }