Provides a high level utility for managing transfers to and from Amazon S3.

TransferUtility provides a simple API for uploading content to and downloading content from Amazon S3. It makes extensive use of Amazon S3 multipart uploads to achieve enhanced throughput, performance, and reliability.

When uploading large files by specifying file paths instead of a stream, TransferUtility uses multiple threads to upload multiple parts of a single upload at once. When dealing with large content sizes and high bandwidth, this can increase throughput significantly.

Transfers are stored in memory. If the application is restarted, previous transfers are no longer accessible. In this situation, if necessary, you should clean up any multipart uploads that are incomplete.

Наследование: ITransferUtility
Пример #1
2
 private async Task ReadObject(Stream stream, CancellationToken ct, string key)
 {
     using (var util = new TransferUtility(_s3))
     {
         using (Stream srcStream = await util.OpenStreamAsync(_bucket, key).ConfigureAwait(false))
         {
             await srcStream.CopyToAsync(stream, 64*1024, ct).ConfigureAwait(false);
         }
     }
 }
 public async Task UploadFile(string name,IStorageFile storageFile)
 {            
     var s3Client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);
     var transferUtilityConfig = new TransferUtilityConfig
     {                
         ConcurrentServiceRequests = 5,                
         MinSizeBeforePartUpload = 20 * MB_SIZE,
     };
     try
     {
         using (var transferUtility = new TransferUtility(s3Client, transferUtilityConfig))
         {
             var uploadRequest = new TransferUtilityUploadRequest
             {
                 BucketName = ExistingBucketName,
                 Key = name,
                 StorageFile = storageFile,
                 // Set size of each part for multipart upload to 10 MB
                 PartSize = 10 * MB_SIZE
             };
             uploadRequest.UploadProgressEvent += OnUploadProgressEvent;
             await transferUtility.UploadAsync(uploadRequest);
         }
     }
     catch (AmazonServiceException ex)
     {
       //  oResponse.OK = false;
      //   oResponse.Message = "Network Error when connecting to AWS: " + ex.Message;
     }
 }
Пример #3
1
        public override IEnumerable<PvcStream> Execute(IEnumerable<PvcStream> inputStreams)
        {
            var filteredInputStreams = FilterUploadedFiles(inputStreams);

            var transfer = new TransferUtility(this.s3client);

            foreach (var inputStream in filteredInputStreams)
            {
                if (inputStream.StreamName == null || inputStream.StreamName.Length == 0)
                    continue;

                var uploadReq = new TransferUtilityUploadRequest();
                uploadReq.BucketName = this.bucketName;
                uploadReq.InputStream = inputStream;
                uploadReq.Key = this.StreamNameToKey(inputStream.StreamName);
                uploadReq.Headers.ContentMD5 = this.keyMD5Sums[uploadReq.Key];

                uploadReq.Headers.ContentType = MimeMapping.GetMimeMapping(inputStream.StreamName);
                if (inputStream.Tags.Contains("gzip"))
                {
                    uploadReq.Headers.ContentEncoding = "gzip";
                }

                transfer.Upload(uploadReq);
            };
            return inputStreams;
        }
Пример #4
1
        static void Main(string[] args)
        {
            
            try
            {
                TransferUtility fT = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));
                string fileKey = genKey();
               

                TransferUtilityUploadRequest uR = new TransferUtilityUploadRequest
                {
                    BucketName = bucketName,
                    FilePath = filepath,
                    CannedACL = S3CannedACL.PublicRead,
                    Key = fileKey

                };
                
                uR.Metadata.Add("Title", "Tiger");
                fT.Upload(uR);
                Console.WriteLine("File Uploaded. Access \"S3.amazonaws.com/sheltdev/" + fileKey );
                Console.ReadKey(false);
            }

            catch (AmazonS3Exception e)
            {
                Console.WriteLine(e.Message, e.InnerException);
                Console.ReadKey(false);
            }

        }
Пример #5
0
 public async Task <S3Response> UploadFile(MemoryStream stream, string path, string bucket)
 {
     try
     {
         using (Amazon.S3.Transfer.TransferUtility transferUti = new Amazon.S3.Transfer.TransferUtility(_client))
         {
             transferUti.Upload(stream, bucket, path);
             return(new S3Response
             {
                 Status = HttpStatusCode.OK,
                 Message = "Enviado com sucesso."
             });
         }
     }
     catch (AmazonS3Exception e)
     {
         return(new S3Response
         {
             Message = e.Message,
             Status = e.StatusCode
         });
     }
     catch (Exception e)
     {
         return(new S3Response
         {
             Status = HttpStatusCode.InternalServerError,
             Message = e.Message
         });
     }
 }
Пример #6
0
 //For testing - allows mocking of files and s3
 public S3FileSystem(AmazonS3 s3Client, Func<string, IFileStreamWrap> fileLoader)
 {
     Logger = new TraceLogger();
     S3Client = s3Client;
     TransferUtility = new TransferUtility(s3Client);
     FileLoader = fileLoader;
 }
Пример #7
0
 public S3FileSystem(IPsCmdletLogger logger, string accessKey, string secret, AmazonS3Config config)
 {
     Logger = logger ?? new TraceLogger();
     S3Client = new AmazonS3Client(accessKey, secret, config);
     TransferUtility = new TransferUtility(S3Client);
     FileLoader = (fileFullName) => new FileWrap().Open(fileFullName, FileMode.Open, FileAccess.ReadWrite);
 }
Пример #8
0
    public string DeleteImageFile(Hashtable State, string url)
    {
        string AWSAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
        string AWSSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
        string Bucket = ConfigurationManager.AppSettings["ImageBucket"];
        TransferUtility transferUtility = new TransferUtility(AWSAccessKey, AWSSecretKey);
        try
        {
            DeleteObjectRequest request = new DeleteObjectRequest();
            string file_name = url.Substring(url.LastIndexOf("/") + 1);
            string key = State["Username"].ToString() + "/" + file_name;
            request.WithBucketName(Bucket)
                .WithKey(key);
            using (DeleteObjectResponse response = transferUtility.S3Client.DeleteObject(request))
            {
                WebHeaderCollection headers = response.Headers;
             }
        }
        catch (AmazonS3Exception ex)
        {
            Util util = new Util();
            util.LogError(State, ex);
            return ex.Message + ": " + ex.StackTrace;
        }

        return "OK";
    }
        public void SendDocument(string filePath, string bucket, string destinationPath, string fileNamOnDestinationWithExtension = "index.html", bool isPublic = false)
        {
            try
            {
                var transferUtility = new TransferUtility(amazonS3Client);
                if (!transferUtility.S3Client.DoesS3BucketExist(bucket))
                    transferUtility.S3Client.PutBucket(new PutBucketRequest { BucketName = bucket });

                var request = new TransferUtilityUploadRequest
                {
                    BucketName = bucket,
                    Key = string.Format("{0}/{1}", destinationPath, fileNamOnDestinationWithExtension),
                    FilePath = filePath
                };
                if (isPublic)
                    request.Headers["x-amz-acl"] = "public-read";
                request.UploadProgressEvent += uploadFileProgressCallback;

                transferUtility.Upload(request);
                transferUtility.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception("Error send file to S3. " + ex.Message);
            }
        }
Пример #10
0
        public void SimpleUpload()
        {
            var client = Client;

            using (var tu = new Amazon.S3.Transfer.TransferUtility(client))
            {
                tu.Upload(fullPath, bucketName);

                var response = client.GetObjectMetadata(new GetObjectMetadataRequest
                {
                    BucketName = bucketName,
                    Key        = testFile
                });
                Assert.IsTrue(response.ETag.Length > 0);

                var downloadPath    = fullPath + ".download";
                var downloadRequest = new Amazon.S3.Transfer.TransferUtilityDownloadRequest
                {
                    BucketName = bucketName,
                    Key        = testFile,
                    FilePath   = downloadPath
                };
                tu.Download(downloadRequest);
                TestDownloadedFile(downloadPath);

                // empty out file, except for 1 byte
                File.WriteAllText(downloadPath, testContent.Substring(0, 1));
                Assert.IsTrue(File.Exists(downloadPath));
                tu.Download(downloadRequest);
                TestDownloadedFile(downloadPath);
            }
        }
Пример #11
0
        public void DirectoryTesting()
        {
            var directory     = TransferUtilityTests.CreateTestDirectory(10 * TransferUtilityTests.KILO_SIZE);
            var keyPrefix     = directory.Name;
            var directoryPath = directory.FullName;

            using (var transferUtility = new Amazon.S3.Transfer.TransferUtility(s3EncryptionClientFileMode))
            {
                TransferUtilityUploadDirectoryRequest uploadRequest = CreateUploadDirRequest(directoryPath, keyPrefix);
                transferUtility.UploadDirectory(uploadRequest);

                var newDir = TransferUtilityTests.GenerateDirectoryPath();
                transferUtility.DownloadDirectory(bucketName, keyPrefix, newDir);
                TransferUtilityTests.ValidateDirectoryContents(s3EncryptionClientFileMode, bucketName, keyPrefix, directory);
            }

            using (var transferUtility = new Amazon.S3.Transfer.TransferUtility(s3EncryptionClientMetadataMode))
            {
                TransferUtilityUploadDirectoryRequest uploadRequest = CreateUploadDirRequest(directoryPath, keyPrefix);
                transferUtility.UploadDirectory(uploadRequest);

                var newDir = TransferUtilityTests.GenerateDirectoryPath();
                transferUtility.DownloadDirectory(bucketName, keyPrefix, newDir);
                TransferUtilityTests.ValidateDirectoryContents(s3EncryptionClientMetadataMode, bucketName, keyPrefix, directory);
            }
        }
Пример #12
0
        public static void UploadFile(System.Tuple<string,string, DateTime> file, string existingBucketName)
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;
            string accessKeyID = appConfig["AWSAccessKey"];
            string secretAccessKey = appConfig["AWSSecretKey"];

            try
            {
                TransferUtility fileTransferUtility = new TransferUtility(accessKeyID, secretAccessKey);

                // Use TransferUtilityUploadRequest to configure options.
                // In this example we subscribe to an event.
                TransferUtilityUploadRequest uploadRequest =
                    new TransferUtilityUploadRequest()
                    .WithBucketName(existingBucketName)
                    .WithFilePath(file.Item1)
                    .WithServerSideEncryptionMethod(ServerSideEncryptionMethod.AES256)
                    .WithKey(file.Item2 + file.Item3.ToString("ddmmyyyymmmmhhss"));

                uploadRequest.UploadProgressEvent +=
                    new EventHandler<UploadProgressArgs>
                        (uploadRequest_UploadPartProgressEvent);

                fileTransferUtility.Upload(uploadRequest);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine(e.Message + e.InnerException);
            }
        }
Пример #13
0
        //Pushes file to Amazon S3 with public read permissions
        public bool UploadFile(string localFile,string fileName, string contentType)
        {
            IAmazonS3 client = GetS3Client();

            var result = false;
            try
            {
                var request = new TransferUtilityUploadRequest
                {
                    BucketName = _BucketName,
                    Key = _Prefix+fileName,
                    FilePath = localFile,
                    StorageClass = S3StorageClass.Standard,
                    CannedACL = S3CannedACL.PublicRead,
                    ContentType = contentType
                };
                var fileTransferUtility = new TransferUtility(client);
                fileTransferUtility.Upload(request);
                //PutObjectResponse response2 = client.PutObject(request);

                result = true;
            }
            catch
            {
                return result;
            }

            return result;
        }
Пример #14
0
        public string UploadFile(string localFilePath, string s3Folder)
        {
            string uploadedFileUrl = string.Empty;

            try
            {
                s3Folder = CorrectFolderPath(s3Folder);

                string key = s3Folder + Path.GetFileName(localFilePath);

                using (var transferUtility = new Amazon.S3.Transfer.TransferUtility(_awsS3Client))
                {
                    using (FileStream fs = new FileStream(localFilePath, FileMode.Open))
                    {
                        var request = new TransferUtilityUploadRequest
                        {
                            BucketName  = _bucketName,
                            Key         = key,
                            InputStream = fs,
                            CannedACL   = S3CannedACL.PublicRead,
                        };

                        transferUtility.Upload(request);
                    }

                    uploadedFileUrl = bucketUrl + key;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("S3 UploadfileToS3 - unexpected exception.", ex);
            }

            return(uploadedFileUrl);
        }
Пример #15
0
        public async Task <string> UploadFileFromStream(Stream stream, string s3Key)
        {
            string uploadedFileUrl = string.Empty;

            try
            {
                using (var transferUtility = new Amazon.S3.Transfer.TransferUtility(_awsS3Client))
                {
                    using (stream)
                    {
                        var request = new TransferUtilityUploadRequest
                        {
                            BucketName  = _bucketName,
                            Key         = s3Key,
                            InputStream = stream,
                            CannedACL   = S3CannedACL.PublicRead,
                        };

                        await transferUtility.UploadAsync(request);
                    }

                    uploadedFileUrl = bucketUrl + s3Key;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("S3 UploadFileFromStream - unexpected exception.", ex);
            }

            return(uploadedFileUrl);
        }
Пример #16
0
 public BloomS3Client(string bucketName)
 {
     _bucketName = bucketName;
     _amazonS3 = AWSClientFactory.CreateAmazonS3Client(KeyManager.S3AccessKey,
         KeyManager.S3SecretAccessKey, new AmazonS3Config { ServiceURL = "https://s3.amazonaws.com" });
     _transferUtility = new TransferUtility(_amazonS3);
 }
Пример #17
0
        public void SetFileToS3(string _local_file_path, string _bucket_name, string _sub_directory, string _file_name_S3)
        {
            // Gelen Değerler :
            // _local_file_path     : Lokal dosya yolu örn. "d:\filename.zip"
            // _bucket_name         : S3 teki bucket adı ,Bucket önceden oluşturulmuş olmalıdır.
            // _sub_directory       : Boş değilse S3 içinde klasör oluşturulur yada varsa içine ekler dosyayı.
            // _file_name_S3        : Dosyanın S3 içindeki adı

            // IAmazonS3 class'ı oluşturuyoruz ,Benim lokasyonum RegionEndpoint.EUCentral1 onun için onu seçiyorum
            // Sizde yüklemek istediğiniz bucket 'ın lokasyonuna göre değiştirmelisiniz.
            IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.EUCentral1);

            // Bir TransferUtility oluşturuyoruz(Türkçesi : Aktarım Programı).
            utility = new TransferUtility(client);
            // TransferUtilityUploadRequest oluşturuyoruz
            request = new TransferUtilityUploadRequest();

            if (_sub_directory == "" || _sub_directory == null)
            {
                request.BucketName = _bucket_name; //Alt klasör girmediysek direk bucket'ın içine atıyor.
            }
            else
            {   // Alt Klasör ve Bucket adı
                request.BucketName = _bucket_name + @"/" + _sub_directory;
            }
            request.Key = _file_name_S3; //Dosyanın S3 teki adı
            request.FilePath = _local_file_path; //Lokal Dosya Yolu
        }
Пример #18
0
        public CraneChatS3Uploader()
        {
            m_CloudFrontRoot = new Uri(ConfigurationManager.AppSettings["CloudFrontRoot"]);
            m_BucketName = ConfigurationManager.AppSettings["BucketName"];

            AmazonS3Config s3Config = new AmazonS3Config().WithServiceURL(ConfigurationManager.AppSettings["S3ServiceURL"].ToString());
            AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(s3Config);
            m_s3transferUtility = new TransferUtility(s3Client);
        }
Пример #19
0
        public AwsFileUpload(String name, String sourceFilePath, Stream stream, String bucketName, String accessKey, String secretKey, string originalFileName, string note, DateTime originalFileDate, bool isDownload, string md5, Int32 duration = 0, string resolution = "")
            : base(name, sourceFilePath, stream, originalFileName, note, originalFileDate, isDownload, duration, resolution)
        {
            this._bucketName = bucketName;

            if (!string.IsNullOrEmpty(md5))
                base._md5 = md5;


            var fileTransferUtilityConfig = new TransferUtilityConfig { ConcurrentServiceRequests = 10 };
            AWSConfigs.S3Config.UseSignatureVersion4 = true;
            string regionFromConfig = System.Configuration.ConfigurationSettings.AppSettings.Get("EndpointRegion");


            RegionEndpoint regionEndpoint = RegionEndpoint.APNortheast1;

            switch (regionFromConfig.ToLower())
            {
                case "apsoutheast1":
                    regionEndpoint = RegionEndpoint.APSoutheast1;
                    break;
                case "apsoutheast2":
                    regionEndpoint = RegionEndpoint.APSoutheast2;
                    break;
                case "cnnorth1":
                    regionEndpoint = RegionEndpoint.CNNorth1;
                    break;
                case "eucentral1":
                    regionEndpoint = RegionEndpoint.EUCentral1;
                    break;
                case "euwest1":
                    regionEndpoint = RegionEndpoint.EUWest1;
                    break;
                case "saeast1":
                    regionEndpoint = RegionEndpoint.SAEast1;
                    break;
                case "useast1":
                    regionEndpoint = RegionEndpoint.USEast1;
                    break;
                case "usgovcloudwest1":
                    regionEndpoint = RegionEndpoint.USGovCloudWest1;
                    break;
                case "uswest1":
                    regionEndpoint = RegionEndpoint.USWest1;
                    break;
                case "uswest2":
                    regionEndpoint = RegionEndpoint.USWest2;
                    break;
                default: break; //APNortheast1
            }


            _amazonS3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), regionEndpoint);
            _amazonS3Client.ExceptionEvent += amazonS3Client_ExceptionEvent;

            _fileTransferUtility = new TransferUtility(_amazonS3Client, fileTransferUtilityConfig);
        }
 public AmazonS3StorageProvider(IAmazonS3StorageConfiguration amazonS3StorageConfiguration)
 {
     _amazonS3StorageConfiguration = amazonS3StorageConfiguration;
     var cred = new BasicAWSCredentials(_amazonS3StorageConfiguration.AWSAccessKey, _amazonS3StorageConfiguration.AWSSecretKey);
     //TODO: aws region to config
     _client = new AmazonS3Client(cred, RegionEndpoint.USEast1);
     var config = new TransferUtilityConfig();
     _transferUtility = new TransferUtility(_client, config);
 }
Пример #21
0
 internal TransferUtility GetTransferUtility(RegionEndpoint region)
 {
     TransferUtility output;
     if (!this.transferUtilitiesByRegion.TryGetValue(region.SystemName, out output))
     {
         output = new TransferUtility(this.GetClient(region));
         this.transferUtilitiesByRegion.Add(region.SystemName, output);
     }
     return output;
 }
Пример #22
0
 protected override void ExecuteS3Task()
 {
     using ( TransferUtility transferUtility = new Amazon.S3.Transfer.TransferUtility( this.AccessKey, this.SecretAccessKey ) ) {
         TransferUtilityDownloadRequest downloadRequest = new TransferUtilityDownloadRequest {
             BucketName = this.BucketName,
             Key = this.SourceFile,
             FilePath = this.DestinationFile,
         };
         // uploadRequest.AddHeader("x-amz-acl", "public-read");
         transferUtility.Download( downloadRequest );
     }
 }
Пример #23
0
 protected override void ExecuteS3Task()
 {
     using (TransferUtility transferUtility = new Amazon.S3.Transfer.TransferUtility(this.AccessKey, this.SecretAccessKey)) {
         TransferUtilityDownloadRequest downloadRequest = new TransferUtilityDownloadRequest {
             BucketName = this.BucketName,
             Key        = this.SourceFile,
             FilePath   = this.DestinationFile,
         };
         // uploadRequest.AddHeader("x-amz-acl", "public-read");
         transferUtility.Download(downloadRequest);
     }
 }
Пример #24
0
 protected void InitAWS()
 {
     //Amazon Configuration (depends on AWSProfileName key on Web.config)
     if (S3Client != null)
     { 
         S3TransferUtil = new TransferUtility(S3Client);
     }
     else
     {
         S3TransferUtil = null;
     }
 }
        public void Upload(FileSources fileSources)
        {
            if (fileSources.Count() > 0)
            {
                utility = new TransferUtility(client);
                foreach (var source in fileSources)
                {
                    parallelTasks.Add(Task.Factory.StartNew(() => StartUpload(source)));
                }
            }

            Task.WaitAll(parallelTasks.ToArray());
        }
Пример #26
0
 public void Dispose()
 {
     if (_transferUtility != null)
     {
         _transferUtility.Dispose();
         _transferUtility = null;
     }
     if (_amazonS3 != null)
     {
         _amazonS3.Dispose();
         _amazonS3 = null;
     }
 }
Пример #27
0
        public HttpResponseMessage ExternalPost()
        {
            HttpResponseMessage result = null;
            HttpRequest httpRequest = HttpContext.Current.Request;
            TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(ConfigService.AwsAccessKeyId
                                , ConfigService.AwsSecretAccessKey
                                , Amazon.RegionEndpoint.USWest2));

            if (httpRequest.Files.Count > 0)
            {
                foreach (string file in httpRequest.Files)
                {
                    HttpPostedFile postedFile = httpRequest.Files[file];

                    string guid = Guid.NewGuid().ToString();

                    string remoteFilePath = ConfigService.RemoteFilePath + guid + "_" + postedFile.FileName;
                    TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                    {
                        BucketName = ConfigService.BucketName,
                        //FilePath = filePath,
                        InputStream = postedFile.InputStream,
                        //StorageClass = S3StorageClass.ReducedRedundancy,
                        //PartSize = 6291456, // 6 MB.
                        Key = remoteFilePath,
                        //CannedACL = S3CannedACL.PublicRead
                    };
                    fileTransferUtility.Upload(fileTransferUtilityRequest);

                    string paraRemoteFilePath = "/" + remoteFilePath;

                    ItemResponse<string> response = new ItemResponse<string>();

                    string userId = UserService.GetCurrentUserId();

                    ProfileService.UpdatePhotoPath(userId, paraRemoteFilePath);

                    response.Item = remoteFilePath;

                    return Request.CreateResponse(HttpStatusCode.Created, response.Item);

                }

            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            return result;
        }
Пример #28
0
        static void Main(string[] args)
        {
            try
            {
                TransferUtility fileTransferUtility = new
                    TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

                // 1. Upload a file, file name is used as the object key name.
                fileTransferUtility.Upload(filePath, existingBucketName);
                Console.WriteLine("Upload 1 completed");

                // 2. Specify object key name explicitly.
                fileTransferUtility.Upload(filePath,
                                          existingBucketName, keyName);
                Console.WriteLine("Upload 2 completed");

                // 3. Upload data from a type of System.IO.Stream.
                using (FileStream fileToUpload =
                    new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    fileTransferUtility.Upload(fileToUpload,
                                               existingBucketName, keyName);
                }
                Console.WriteLine("Upload 3 completed");

                // 4.Specify advanced settings/options.
                TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                {
                    BucketName = existingBucketName,
                    FilePath = filePath,
                    StorageClass = S3StorageClass.ReducedRedundancy,
                    PartSize = 5242880, // 5 MB.
                    Key = keyName,
                    CannedACL = S3CannedACL.PublicRead
                };
                fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
                fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
                fileTransferUtility.Upload(fileTransferUtilityRequest);
                Console.WriteLine("Upload 4 completed");
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message,
                                  s3Exception.InnerException);
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
Пример #29
0
 public void Upload(string filePath)
 {
     try
     {
         TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));
         using (FileStream fileToUpload = new FileStream(filePath, FileMode.Open, FileAccess.Read))
         {
             fileTransferUtility.Upload(fileToUpload, bucketName, keyName);
         }
     }
     catch (AmazonS3Exception ex)
     {
         Log.Error(ex.Message, ex.InnerException);
     }
 }
Пример #30
0
        public static void uploadFile(string filePath, string existingBucketName, string AccessKey, string SecretKey, string sessionToken)
        {

            Console.WriteLine("filepath: "+filePath);
            Console.WriteLine("bucketname: " + existingBucketName);
            Console.WriteLine("ak: " + AccessKey);
            Console.WriteLine("sk: " + SecretKey);
            Console.WriteLine("st: " + sessionToken);
            try
            {
                AmazonS3Client sclient = new AmazonS3Client(AccessKey, SecretKey, sessionToken, Amazon.RegionEndpoint.USWest2);
                TransferUtility fileTransferUtility = new TransferUtility(sclient);

                /* Find way to increase time out timer because of large file size
                TransferUtilityConfig config = new TransferUtilityConfig();
                config.DefaultTimeout = 11111;
                TransferUtility utility = new TransferUtility(config);
                */
                // 1. Upload a file, file name is used as the object key name.
               fileTransferUtility.Upload(filePath, existingBucketName);
               Console.WriteLine("Upload 1 completed");
                
                /*
                // 2. Specify object key name explicitly.
                fileTransferUtility.Upload(filePath,existingBucketName, keyName);
                Console.WriteLine("Upload 2 completed");

              

                // 4.Specify advanced settings/options.
                TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                {
                    BucketName = existingBucketName,
                    FilePath = filePath,
                    StorageClass = S3StorageClass.ReducedRedundancy,
                    PartSize = 6291456, // 6 MB.
                    Key = keyName
                };
                fileTransferUtility.Upload(fileTransferUtilityRequest);
                Console.WriteLine("Upload completed");
                 * */
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message,
                                  s3Exception.InnerException);
            }
        }
        /// <summary>
        /// This method loads the AWSProfileName that is set in the App.config and creates the transfer utility.
        /// </summary>
        private void loadConfiguration()
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            if (string.IsNullOrEmpty(appConfig["AWSProfileName"]))
            {
                MessageBox.Show(this, "AWSProfileName is not set in the App.Config", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                this.Close();
                return;
            }

            this._transferUtility = new TransferUtility(RegionEndpoint.USWest2);

            // Update the Bucket to the optionally supplied Bucket from the App.config.
            this.Bucket = appConfig["Bucket"];
        }
Пример #32
0
        public IPhoto UploadPhoto(Stream stream, string filename, string title, string descriptioSn, string tags)
        {
            TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
            request.InputStream = stream;
            request.BucketName = photoBucket;
            request.Key = filename;
            request.CannedACL = Amazon.S3.Model.S3CannedACL.PublicRead;

            TransferUtility transferUtility = new TransferUtility(ConfigurationManager.AppSettings["AWSAccessKey"], ConfigurationManager.AppSettings["AWSSecretKey"]);
            transferUtility.Upload(request);

            S3Photo photo = new S3Photo();
            photo.WebUrl = string.Format("http://s3.amazonaws.com/{0}/{1}", photoBucket, filename);
            photo.Title = filename;

            return photo;
        }
Пример #33
0
        ///////////////////////////////////////////////////////////////////////
        //                    Methods & Functions                            //
        ///////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Class constructor
        /// </summary>
        public AWSS3Helper(RegionEndpoint regionendpoint)
        {
            // Set configuration info
            AmazonS3Config config = new AmazonS3Config();
            config.Timeout = new TimeSpan(1, 0, 0);
            config.ReadWriteTimeout = new TimeSpan(1, 0, 0);
            config.RegionEndpoint = regionendpoint;

            // Create S3 client
            S3client = Amazon.AWSClientFactory.CreateAmazonS3Client
                        (Gadgets.LoadConfigurationSetting("AWSAccessKey", ""),
                         Gadgets.LoadConfigurationSetting("AWSSecretKey", ""),
                         config);

            // Create the file transfer utility class
            fileTransferUtility = new TransferUtility(S3client);
        }
        private void UploadFileToS3(string filePath,string bucketname)
        {
            var awsAccessKey = accesskey;
            var awsSecretKey = secretkey;
            var existingBucketName = bucketname;
            var client =  Amazon.AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey, RegionEndpoint.USEast1);

            var uploadRequest = new TransferUtilityUploadRequest
            {
                FilePath = filePath,
                BucketName = existingBucketName,
                CannedACL = S3CannedACL.PublicRead
            };

            var fileTransferUtility = new TransferUtility(client);
            fileTransferUtility.UploadAsync(uploadRequest);
        } 
        protected override void InnerExecute(string[] arguments)
        {
            _writer.WriteLine("Getting upload credentials... ");
            _writer.WriteLine();

            var uploadCredentials = GetCredentials();

            var temporaryFileName = Path.GetTempFileName();
            try
            {
                using (var packageStream = new FileStream(temporaryFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                using (var gzipStream = new GZipStream(packageStream, CompressionMode.Compress, true))
                {
                    var sourceDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
                    sourceDirectory.ToTar(gzipStream, excludedDirectoryNames: _excludedDirectories.ToArray());
                }

                using (var s3Client = new AmazonS3Client(uploadCredentials.GetSessionCredentials()))
                using (var transferUtility = new TransferUtility(s3Client))
                {
                    var request = new TransferUtilityUploadRequest
                    {
                        FilePath = temporaryFileName,
                        BucketName = uploadCredentials.Bucket,
                        Key = uploadCredentials.ObjectKey,
                        Timeout = (int)TimeSpan.FromHours(2).TotalMilliseconds,
                    };

                    var progressBar = new MegaByteProgressBar();
                    request.UploadProgressEvent += (object x, UploadProgressArgs y) => progressBar
                        .Update("Uploading package", y.TransferredBytes, y.TotalBytes);

                    transferUtility.Upload(request);

                    Console.CursorTop++;
                    _writer.WriteLine();
                }
            }
            finally
            {
                File.Delete(temporaryFileName);
            }

            TriggerAppHarborBuild(uploadCredentials);
        }
Пример #36
0
        protected override void ExecuteS3Task()
        {
            if (!File.Exists(this.SourceFile))
            {
                throw new BuildException("source-file does not exist: " + this.SourceFile);
            }

            using (TransferUtility transferUtility = new Amazon.S3.Transfer.TransferUtility(this.AccessKey, this.SecretAccessKey)) {
                TransferUtilityUploadRequest uploadRequest = new TransferUtilityUploadRequest {
                    BucketName = this.BucketName,
                    FilePath   = this.SourceFile,
                    Key        = this.DestinationFile
                };
                if (PublicRead)
                {
                    uploadRequest.AddHeader("x-amz-acl", "public-read");
                }
                transferUtility.Upload(uploadRequest);
            }
        }
Пример #37
0
        private static async System.Threading.Tasks.Task <string> UploadFile(string fileName, Stream fileStream)
        {
            var extension = Path.GetExtension(fileName);

            var client   = new AmazonS3Client("AKIAJNOS24TJ3PWZHKEQ", "+d+qIQ5Uv8dfFTdsdvBd0Hp0Exm5QY2YH1ZL8903", RegionEndpoint.USWest2);
            var transfer = new Amazon.S3.Transfer.TransferUtility(client);

            var request = new TransferUtilityUploadRequest();

            request.BucketName  = "sakjfkls-test-bucket";
            request.InputStream = new MemoryStream();
            request.Key         = Guid.NewGuid().ToString() + extension;
            request.InputStream = fileStream;

            request.CannedACL = S3CannedACL.AuthenticatedRead;

            await transfer.UploadAsync(request);

            return(request.Key);
        }
Пример #38
0
        public async Task <S3Response> CopyObjectAsync(string pathOrigem, string pathDestino, string bucketOrigem, string bucketDestino)
        {
            try
            {
                using (Amazon.S3.Transfer.TransferUtility transferUti = new Amazon.S3.Transfer.TransferUtility(_client))
                {
                    CopyObjectRequest request = new CopyObjectRequest
                    {
                        SourceBucket      = bucketOrigem,
                        SourceKey         = pathOrigem,
                        DestinationBucket = bucketDestino,
                        DestinationKey    = pathDestino
                    };
                    await _client.CopyObjectAsync(request);

                    return(new S3Response
                    {
                        Status = HttpStatusCode.OK,
                        Message = "Enviado com sucesso."
                    });
                }
            }
            catch (AmazonS3Exception e)
            {
                return(new S3Response
                {
                    Message = e.Message,
                    Status = e.StatusCode
                });
            }
            catch (Exception e)
            {
                return(new S3Response
                {
                    Status = HttpStatusCode.InternalServerError,
                    Message = e.Message
                });
            }
        }
Пример #39
0
        public async Task <S3Response> DeleteFile(string bucketName, string pathAmazon)
        {
            try
            {
                using (Amazon.S3.Transfer.TransferUtility transferUti = new Amazon.S3.Transfer.TransferUtility(_client))
                {
                    var deleteObjectRequest = new DeleteObjectRequest
                    {
                        BucketName = bucketName,
                        Key        = pathAmazon
                    };

                    await _client.DeleteObjectAsync(deleteObjectRequest);

                    return(new S3Response
                    {
                        Status = HttpStatusCode.OK,
                        Message = "Enviado com sucesso."
                    });
                }
            }
            catch (AmazonS3Exception e)
            {
                return(new S3Response
                {
                    Message = e.Message,
                    Status = e.StatusCode
                });
            }
            catch (Exception e)
            {
                return(new S3Response
                {
                    Status = HttpStatusCode.InternalServerError,
                    Message = e.Message
                });
            }
        }
Пример #40
0
        public void SimpleUpload()
        {
            var client = Client;

            using (var tu = new Amazon.S3.Transfer.TransferUtility(client))
            {
                tu.Upload(testFilePath, testBucketName);

                var response = WaitUtils.WaitForComplete(
                    () =>
                {
                    return(client.GetObjectMetadataAsync(new GetObjectMetadataRequest
                    {
                        BucketName = testBucketName,
                        Key = TEST_FILENAME
                    }).Result);
                });
                Assert.True(response.ETag.Length > 0);

                var downloadPath    = testFilePath + ".download";
                var downloadRequest = new Amazon.S3.Transfer.TransferUtilityDownloadRequest
                {
                    BucketName = testBucketName,
                    Key        = TEST_FILENAME,
                    FilePath   = downloadPath
                };
                tu.Download(downloadRequest);
                TestDownloadedFile(downloadPath);

                // empty out file, except for 1 byte
                File.WriteAllText(downloadPath, TEST_CONTENT.Substring(0, 1));
                Assert.True(File.Exists(downloadPath));
                tu.Download(downloadRequest);
                TestDownloadedFile(downloadPath);
            }
        }
Пример #41
0
 public S3Uploader(string bucketName)
 {
     this.bucketName      = bucketName;
     this.transferUtility = new Amazon.S3.Transfer.TransferUtility("AKIAIALOFNWOTXDMVF3Q", "d0mcWo3UkDD95rE9KyFxowbmPnr9t1Y4RbmHvwGA");
 }
Пример #42
-1
        public async Task DownloadTestFile()
        {
            var token = await S3Service.GetS3Token("upload");

                string filePath = "c:/temp/download/test.jpg";
                string awsAccessKeyId = token.accessKeyId;
                string awsSecretAccessKey = token.secretAccessKey;
                string awsSessionToken = token.sessionToken;
                string existingBucketName = token.bucket;
                string keyName = string.Format("{0}/{1}", token.path, Path.GetFileName(filePath));

                var client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, awsSessionToken, RegionEndpoint.APSoutheast2);
                var fileTransferUtility = new TransferUtility(client);

                var request = new TransferUtilityDownloadRequest()
                {
                    BucketName = existingBucketName,
                    FilePath = filePath,
                    Key = keyName,
                    ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
                    ServerSideEncryptionCustomerProvidedKey = token.uploadPassword,
                };

                fileTransferUtility.Download(request);
                Console.WriteLine("download 1 completed");
        }