public void SimpleUploadWithRequest()
        {
            RunAsSync(async() =>
            {
                try
                {
                    var client = Client;
                    using (var tu = new Amazon.S3.Transfer.TransferUtility(client))
                    {
                        string key = System.Guid.NewGuid().ToString();

                        Amazon.S3.Transfer.TransferUtilityUploadRequest request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
                        {
                            BucketName   = bucketName,
                            FilePath     = fullPath,
                            StorageClass = S3StorageClass.ReducedRedundancy,
                            PartSize     = 6291456, // 6 MB.
                            Key          = key,
                            CannedACL    = S3CannedACL.PublicRead
                        };
                        Stream filestream = this.BaseFolder.GetFileAsync(fullPath).Result.OpenAsync(FileAccess.Read).Result;
                        await tu.UploadAsync(fullPath, bucketName, key);
                        await VerifyUploadResponse(client, key);
                        await TestDownloadedFile(fullPath, tu, key);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            });
        }
Пример #2
0
 public Task UploadFromAsync(IStorageFile sourceFile, CancellationToken cancellationToken = default(CancellationToken))
 {
     var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
     request.BucketName = this.linker.s3.bucket;
     request.Key = this.linker.s3.key;
     request.StorageFile = sourceFile;
     return GetTransferUtility().UploadAsync(request, cancellationToken);
 }
Пример #3
0
        public Task UploadFromAsync(IStorageFile sourceFile, CancellationToken cancellationToken = default(CancellationToken))
        {
            var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest();

            request.BucketName  = this.linker.s3.bucket;
            request.Key         = this.linker.s3.key;
            request.StorageFile = sourceFile;
            return(GetTransferUtility().UploadAsync(request, cancellationToken));
        }
Пример #4
0
        public override Uri UploadWithoutQuota(string domain, string path, Stream stream, string contentType, string contentDisposition)
        {
            ACL acl = ACL.Auto;

            using (AmazonS3 client = GetClient())
            {
                var util = new Amazon.S3.Transfer.TransferUtility(client);

                var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest();

                string mime = string.IsNullOrEmpty(contentType)
                                  ? MimeMapping.GetMimeMapping(Path.GetFileName(path))
                                  : contentType;

                const int uploadTimeout = 10 * 60 * 1000;

                request.BucketName  = _bucket;
                request.Key         = MakePath(domain, path);
                request.CannedACL   = acl == ACL.Auto ? GetDomainACL(domain) : GetS3Acl(acl);
                request.ContentType = mime;
                request.Timeout     = uploadTimeout;

                var headers = new NameValueCollection();
                headers.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(5).TotalSeconds));
                headers.Add("Etag", (DateTime.UtcNow.Ticks).ToString(CultureInfo.InvariantCulture));
                headers.Add("Last-Modified", DateTime.UtcNow.ToString("R"));
                headers.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(5)).ToString("R"));
                if (!string.IsNullOrEmpty(contentDisposition))
                {
                    headers.Add("Content-Disposition", Uri.EscapeDataString(contentDisposition));
                }
                else if (mime == "application/octet-stream")
                {
                    headers.Add("Content-Disposition", "attachment");
                }

                request.AddHeaders(headers);

                //Send body
                var buffered = stream.GetBuffered();

                request.AutoCloseStream = false;

                request.InputStream = buffered;

                util.Upload(request);

                InvalidateCloudFront(MakePath(domain, path));

                return(GetUri(domain, path));
            }
        }
        void ICoreAmazonS3.UploadObjectFromFilePath(string bucketName, string objectKey, string filepath, IDictionary <string, object> additionalProperties)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key        = objectKey,
                FilePath   = filepath
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            transfer.Upload(request);
        }
        Task ICoreAmazonS3.UploadObjectFromStreamAsync(string bucketName, string objectKey, Stream stream, IDictionary <string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName  = bucketName,
                Key         = objectKey,
                InputStream = stream
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            return(transfer.UploadAsync(request, cancellationToken));
        }
        void ICoreAmazonS3.UploadObjectFromStream(string bucketName, string objectKey, Stream stream, IDictionary <string, object> additionalProperties)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName  = bucketName,
                Key         = objectKey,
                InputStream = stream
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);
            transfer.Upload(request);
        }
        IAsyncResult ICoreAmazonS3.BeginUploadObjectFromFilePath(string bucketName, string objectKey, string filepath, IDictionary <string, object> additionalProperties, AsyncCallback callback, object state)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request  = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key        = objectKey,
                FilePath   = filepath
            };

            InternalSDKUtils.ApplyValues(request, additionalProperties);

            return(transfer.BeginUpload(request, callback, state));
        }
Пример #9
0
        public bool SaveFile(string sFolder, string sObjectKey, byte[] fileContent, bool bMakePublic)
        {
            try
            {
                AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(S3ACCESSKEY, S3SECRETKEY);
                Amazon.S3.Transfer.TransferUtility uploader = new Amazon.S3.Transfer.TransferUtility(S3ACCESSKEY, S3SECRETKEY);
                string BUCKET_NAME = ConfigurationManager.AppSettings["AWSBUCKET"];

                ListBucketsResponse response = client.ListBuckets();
                bool found = false;
                foreach (S3Bucket bucket in response.Buckets)
                {
                    if (bucket.BucketName == BUCKET_NAME)
                    {
                        found = true;
                        break;
                    }
                }
                if (found == false)
                {
                    client.PutBucket(new PutBucketRequest().WithBucketName(BUCKET_NAME));
                }

                if (sFolder != "")
                {
                    sObjectKey = sFolder + "/" + sObjectKey;
                }

                System.IO.Stream stream = new System.IO.MemoryStream(fileContent);
                Amazon.S3.Transfer.TransferUtilityUploadRequest request = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
                request.WithBucketName(BUCKET_NAME);
                request.WithKey(sObjectKey);
                request.WithInputStream(stream);
                request.WithTimeout(-1);
                if (bMakePublic)
                {
                    request.CannedACL = S3CannedACL.PublicRead;
                }
                uploader.Upload(request);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Пример #10
0
        public override string SavePrivate(string domain, string path, Stream stream, DateTime expires)
        {
            using (AmazonS3 client = GetClient())
            {
                var objectKey = MakePath(domain, path);

                var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
                {
                    BucketName  = _bucket,
                    Key         = objectKey,
                    CannedACL   = S3CannedACL.BucketOwnerFullControl,
                    ContentType = "application/octet-stream",
                };

                request.WithMetadata("private-expire", expires.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture));

                var headers = new NameValueCollection();
                headers.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(5).TotalSeconds));
                headers.Add("Etag", (DateTime.UtcNow.Ticks).ToString(CultureInfo.InvariantCulture));
                headers.Add("Last-Modified", DateTime.UtcNow.ToString("R"));
                headers.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(5)).ToString("R"));
                headers.Add("Content-Disposition", "attachment");
                request.AddHeaders(headers);

                request.InputStream = stream;
                new Amazon.S3.Transfer.TransferUtility(client).Upload(request);

                //Get presigned url
                var pUrlRequest = new GetPreSignedUrlRequest
                {
                    BucketName = _bucket,
                    Expires    = expires,
                    Key        = objectKey,
                    Protocol   = Protocol.HTTP,
                    Verb       = HttpVerb.GET
                };

                string url = client.GetPreSignedURL(pUrlRequest);
                //TODO: CNAME!
                return(url);
            }
        }
 public void UploadFile(string filePath, string toPath)
 {
     try
     {
         AsyncCallback callback = new AsyncCallback(uploadComplete);
         log.Info("S3 upload started...");
         log.InfoFormat("S3 filePath: {0}", filePath);
         log.InfoFormat("S3 toPath: {0}", toPath);
         var uploadRequest = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
         uploadRequest.FilePath     = filePath;
         uploadRequest.BucketName   = bucketName;
         uploadRequest.Key          = toPath;
         uploadRequest.StorageClass = Amazon.S3.Model.S3StorageClass.ReducedRedundancy;
         uploadRequest.AddHeader("x-amz-acl", "public-read");
         IAsyncResult ar = transferUtility.BeginUpload(uploadRequest, callback, null);
         transferUtility.EndUpload(ar);
     }
     catch (AmazonS3Exception amazonS3Exception)
     {
         log.ErrorFormat("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
     }
 }
 void ICoreAmazonS3.UploadObjectFromStream(string bucketName, string objectKey, Stream stream, IDictionary<string, object> additionalProperties)
 {
     var transfer = new Amazon.S3.Transfer.TransferUtility(this);
     var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
     {
         BucketName = bucketName,
         Key = objectKey,
         InputStream = stream
     };
     InternalSDKUtils.ApplyValues(request, additionalProperties);
     transfer.Upload(request);
 }
Пример #13
0
        IAsyncResult ICoreAmazonS3.BeginUploadObjectFromFilePath(string bucketName, string objectKey, string filepath, IDictionary<string, object> additionalProperties, AsyncCallback callback, object state)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key = objectKey,
                FilePath = filepath
            };
            InternalSDKUtils.ApplyValues(request, additionalProperties);

            return transfer.BeginUpload(request, callback, state);
        }
        Task ICoreAmazonS3.UploadObjectFromFilePathAsync(string bucketName, string objectKey, string filepath, IDictionary<string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key = objectKey,
                FilePath = filepath
            };
            InternalSDKUtils.ApplyValues(request, additionalProperties);

            return transfer.UploadAsync(request, cancellationToken);
        }
        async Task ICoreAmazonS3.UploadObjectFromStreamAsync(string bucketName, string objectKey, Stream stream, IDictionary<string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                Key = objectKey,
                InputStream = stream
            };
            InternalSDKUtils.ApplyValues(request, additionalProperties);

            await transfer.UploadAsync(request, cancellationToken).ConfigureAwait(false);
        }
 void ICoreAmazonS3.UploadObjectFromFilePath(string bucketName, string objectKey, string filepath, IDictionary<string, object> additionalProperties)
 {
     var transfer = new Amazon.S3.Transfer.TransferUtility(this);
     var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
     {
         BucketName = bucketName,
         Key = objectKey,
         FilePath = filepath
     };
     InternalSDKUtils.ApplyValues(request, additionalProperties);
     transfer.Upload(request);
 }
Пример #17
0
        public override string SavePrivate(string domain, string path, Stream stream, DateTime expires)
        {
            using (AmazonS3 client = GetClient())
            {
                var objectKey = MakePath(domain, path);

                var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest
                    {
                        BucketName = _bucket,
                        Key = objectKey,
                        CannedACL = S3CannedACL.BucketOwnerFullControl,
                        ContentType = "application/octet-stream",

                    };

                request.WithMetadata("private-expire", expires.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture));

                var headers = new NameValueCollection();
                headers.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(5).TotalSeconds));
                headers.Add("Etag", (DateTime.UtcNow.Ticks).ToString(CultureInfo.InvariantCulture));
                headers.Add("Last-Modified", DateTime.UtcNow.ToString("R"));
                headers.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(5)).ToString("R"));
                headers.Add("Content-Disposition", "attachment");
                request.AddHeaders(headers);

                request.InputStream = stream;
                new Amazon.S3.Transfer.TransferUtility(client).Upload(request);

                //Get presigned url                
                var pUrlRequest = new GetPreSignedUrlRequest
                {
                    BucketName = _bucket,
                    Expires = expires,
                    Key = objectKey,
                    Protocol = Protocol.HTTP,
                    Verb = HttpVerb.GET
                };

                string url = client.GetPreSignedURL(pUrlRequest);
                //TODO: CNAME!
                return url;
            }
        }
Пример #18
-2
        public override Uri UploadWithoutQuota(string domain, string path, Stream stream, string contentType, string contentDisposition)
        {
            ACL acl = ACL.Auto;
            using (AmazonS3 client = GetClient())
            {
                var util = new Amazon.S3.Transfer.TransferUtility(client);

                var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest();

                string mime = string.IsNullOrEmpty(contentType)
                                  ? MimeMapping.GetMimeMapping(Path.GetFileName(path))
                                  : contentType;

                const int uploadTimeout = 10 * 60 * 1000;

                request.BucketName = _bucket;
                request.Key = MakePath(domain, path);
                request.CannedACL = acl == ACL.Auto ? GetDomainACL(domain) : GetS3Acl(acl);
                request.ContentType = mime;
                request.Timeout = uploadTimeout;

                var headers = new NameValueCollection();
                headers.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(5).TotalSeconds));
                headers.Add("Etag", (DateTime.UtcNow.Ticks).ToString(CultureInfo.InvariantCulture));
                headers.Add("Last-Modified", DateTime.UtcNow.ToString("R"));
                headers.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(5)).ToString("R"));
                if (!string.IsNullOrEmpty(contentDisposition))
                {
                    headers.Add("Content-Disposition", Uri.EscapeDataString(contentDisposition));
                }
                else if (mime == "application/octet-stream")
                {
                    headers.Add("Content-Disposition", "attachment");
                }

                request.AddHeaders(headers);

                //Send body
                var buffered = stream.GetBuffered();

                request.AutoCloseStream = false;

                request.InputStream = buffered;

                util.Upload(request);

                InvalidateCloudFront(MakePath(domain, path));

                return GetUri(domain, path);
            }
        }
        Task ICoreAmazonS3.UploadObjectFromStorageAsync(string bucketName, string objectKey, IStorageFile storageFile, IDictionary<string, object> additionalProperties, CancellationToken cancellationToken)
        {
            var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
            request.BucketName = bucketName;
            request.Key = objectKey;
            request.StorageFile = storageFile;
            InternalSDKUtils.ApplyValues(request, additionalProperties);

            var transfer = new Amazon.S3.Transfer.TransferUtility(this);
            return transfer.UploadAsync(request, cancellationToken);
        }