Beispiel #1
0
 public async Task UploadFileToS3(IFormFile file)
 {
     using (var client = new AmazonS3Client(BucketInfo.AWSKey, BucketInfo.AWSSKey, RegionEndpoint.USEast2))
     {
         using (var newMemoryStream = new MemoryStream())
         {
             file.CopyTo(newMemoryStream);
             var uploadRequest = new TransferUtilityUploadRequest
             {
                 InputStream = newMemoryStream,
                 Key         = file.FileName,
                 BucketName  = BucketInfo.Bucket,
                 CannedACL   = S3CannedACL.PublicRead
             };
             var fileTransferUtility = new TransferUtility(client);
             await fileTransferUtility.UploadAsync(uploadRequest);
         }
     }
 }
Beispiel #2
0
        public HttpResponseMessage UploadFile()
        {
            var    httpPostedFile = HttpContext.Current.Request.Files[0];
            string fileName       = Path.GetFileNameWithoutExtension(httpPostedFile.FileName);
            string extension      = Path.GetExtension(httpPostedFile.FileName);
            var    newGuid        = Guid.NewGuid().ToString("");
            var    newfileName    = fileName + "_" + newGuid + extension;
            Stream st             = httpPostedFile.InputStream;

            try
            {
                if (httpPostedFile != null)
                {
                    TransferUtility utility = new TransferUtility(awsS3Client);
                    TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
                    request.BucketName  = bucketname;
                    request.Key         = newfileName;
                    request.InputStream = st;
                    utility.Upload(request); //File Streamed to AWS
                }
                if (ModelState.IsValid)
                {
                    FileUploadService    svc   = new FileUploadService();
                    FileUploadAddRequest model = new FileUploadAddRequest();
                    model.FileTypeId     = 1;
                    model.UserFileName   = fileName;
                    model.SystemFileName = newfileName;
                    model.Location       = "https://sabio-training.s3.us-west-2.amazonaws.com/Test/" + newfileName;
                    int?id = svc.Insert(model);
                    ItemResponse <int?> resp = new ItemResponse <int?>();
                    resp.Item = id;
                    return(Request.CreateResponse(HttpStatusCode.OK, resp));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Beispiel #3
0
        public async Task UploadFileAsync(string filePath)
        {
            try
            {
                var fileTransferUtility =
                    new TransferUtility(_s3Client);

                // Option 1. Upload a file. The file name is used as the object key name.
                await fileTransferUtility.UploadAsync(filePath, BucketName);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
Beispiel #4
0
        // Upload local file to bucket
        // Returns true on success, false on error.

        public override bool UploadFile(String bucket, String file)
        {
            try
            {
                this.Exception = null;
                TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(this.AccessKey, this.SecretKey, this.RegionEndpoint));
                fileTransferUtility.Upload(file, bucket);
                return(true);
            }
            catch (Exception ex)
            {
                this.Exception = ex;
                if (!this.HandleErrors)
                {
                    throw ex;
                }
                return(false);
            }
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            try {
                TransferUtility fileTransferUtility = new
                                                      TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast2));

                // 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     = 6291456, // 6 MB.
                    Key          = keyName,
                    CannedACL    = S3CannedACL.PublicRead
                };
                fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
                fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
                fileTransferUtility.Upload(fileTransferUtilityRequest);
                Console.WriteLine("Upload 4 completed");
                Console.Read();
            } catch (AmazonS3Exception s3Exception) {
                Console.WriteLine(s3Exception.Message,
                                  s3Exception.InnerException);
            }
        }
Beispiel #6
0
        public static async Task <String> UploadS3File(string bucket, string region, string access, string secret, IFormFile file, string filename)
        {
            try
            {
                var credentials = new BasicAWSCredentials(access, secret);
                var config      = new AmazonS3Config


                {
                    RegionEndpoint = andys.function.S3Region.getAWSRegion(region)
                };
                using var client = new AmazonS3Client(credentials, config);
                await using var newMemoryStream = new MemoryStream();
                file.CopyTo(newMemoryStream);

                var uploadRequest = new TransferUtilityUploadRequest
                {
                    InputStream = newMemoryStream,
                    Key         = filename,
                    BucketName  = bucket,
                    CannedACL   = S3CannedACL.PublicRead
                };

                var fileTransferUtility = new TransferUtility(client);
                await fileTransferUtility.UploadAsync(uploadRequest);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Incorrect AWS Credentials.");
                    return("Incorrect AWS Credentials.");
                }
                else
                {
                    Console.WriteLine("Error: ", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
                    return(("Error: ", amazonS3Exception.ErrorCode, amazonS3Exception.Message).ToString());
                }
            }

            return(filename);
        }
Beispiel #7
0
        private void TestUploadDirectory(string bucketName, string keyId)
        {
            var directoryName = UtilityMethods.GenerateName("UploadDirectoryTest");

            var directoryPath = Path.Combine(basePath, directoryName);

            for (int i = 0; i < 5; i++)
            {
                var filePath = Path.Combine(Path.Combine(directoryPath, i.ToString()), "file.txt");
                UtilityMethods.WriteFile(filePath, fileContents);
            }

            var config = new TransferUtilityConfig
            {
                ConcurrentServiceRequests = 10,
            };
            var transferUtility = new TransferUtility(Client, config);
            var request         = new TransferUtilityUploadDirectoryRequest
            {
                BucketName    = bucketName,
                Directory     = directoryPath,
                KeyPrefix     = directoryName,
                SearchPattern = "*",
                SearchOption  = SearchOption.AllDirectories,
                ServerSideEncryptionMethod = ServerSideEncryptionMethod.AWSKMS,
                ServerSideEncryptionKeyManagementServiceKeyId = keyId
            };

            HashSet <string> keys = new HashSet <string>();

            request.UploadDirectoryFileRequestEvent += (s, e) =>
            {
                keys.Add(e.UploadRequest.Key);
            };
            transferUtility.UploadDirectory(request);
            Assert.AreEqual(5, keys.Count);

            foreach (var key in keys)
            {
                VerifyObject(bucketName, key, keyId);
            }
        }
Beispiel #8
0
        public async Task <S3Response> UploadFileAsync(MemoryStream memoryStream, string BucketName, string FileKey)
        {
            if (memoryStream == null)
            {
                return new S3Response {
                           Message = "memoryStream value null"
                }
            }
            ;
            try
            {
                var fileTransferUtility = new TransferUtility(_client);

                await fileTransferUtility.UploadAsync(memoryStream, BucketName, FileKey);

                //await fileTransferUtility.UploadAsync(File, BucketName, "Test");
                return(new S3Response
                {
                    Message = "Upload Successfull",
                    UploadSuccessful = true
                });
            }
            catch (AmazonS3Exception e)
            {
                return(new S3Response
                {
                    Message = e.Message,
                    Status = e.StatusCode,
                    UploadSuccessful = false
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(new S3Response
                {
                    Message = e.Message,
                    UploadSuccessful = false
                });
            }
        }
    }
        /// <summary>
        /// Get file from s3 bucket
        /// </summary>
        /// <param name="bucketName">bucket name</param>
        /// <param name="key">s3 key -  path + file name</param>
        /// <returns><see cref="MemoryStream"/>Memory file object</returns>
        public async Task <MemoryStream> DownloadMemoryStreamAsync(string bucketName, string key)
        {
            var memory = new MemoryStream();

            try
            {
                var streamRequest = new TransferUtilityOpenStreamRequest
                {
                    BucketName = bucketName,
                    Key        = key
                };

                var request = new GetObjectRequest()
                {
                    BucketName = bucketName,
                    Key        = key
                };

                using (var transferUtility = new TransferUtility(_s3Client))
                {
                    var objectResponse = await transferUtility.S3Client.GetObjectAsync(request);

                    var stream = objectResponse.ResponseStream;

                    await stream.CopyToAsync(memory);
                }

                memory.Position = 0;

                return(memory);
            }
            catch (AmazonS3Exception e)
            {
                Console.Write("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.Write("Download fail", e.Message);
            }

            return(memory);
        }
Beispiel #10
0
        /// <summary>
        /// send file to s3
        /// </summary>
        /// <param name="localFilePath"></param>
        /// <param name="bucketName"></param>
        /// <param name="subDirectoryInBucket"></param>
        /// <param name="fileNameInS3"></param>
        /// <returns></returns>
        public bool sendMyFileToS3(string localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
        {
            try
            {
                AmazonS3Config config = new AmazonS3Config
                {
                    ServiceURL     = endPoint,
                    ForcePathStyle = true
                };

                IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey, config);
                //IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.EUWest1);

                // create a TransferUtility instance passing it the IAmazonS3 created in the first step
                TransferUtility utility = new TransferUtility(client);
                // making a TransferUtilityUploadRequest instance
                TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();

                if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
                {
                    request.BucketName = bucketName; //no subdirectory just bucket name
                }
                else
                {   // subdirectory and bucket name
                    request.BucketName = bucketName + @"/" + subDirectoryInBucket;
                }
                request.Key          = fileNameInS3;  //file name up in S3
                request.FilePath     = localFilePath; //local file name
                request.CannedACL    = S3CannedACL.PublicRead;
                request.StorageClass = S3StorageClass.ReducedRedundancy;
                utility.Upload(request); //commensing the transfer
            }
            catch (AmazonS3Exception ex)
            {
                //Console.Write(ex.ToString());
                Ultils.AddToLogFile("ex" + ex.ToString());
                ////Console.WriteLine(s3Exception.Message,
                //                  s3Exception.InnerException);
                return(false);
            }
            return(true); //indicate that the file was sent
        }
        public async Task UploadImageToS3(ImageModel imageToUpload)
        {
            var imagesBucketName     = $"helpet/{_environment.EnvironmentName}/images/{imageToUpload.Folder}";
            var thumbnailsBucketName = $"helpet/{_environment.EnvironmentName}/images/{imageToUpload.Folder}/thumbnails";

            var client = new AmazonS3Client(_generalSettings.S3AccessKey, _generalSettings.S3SecretKey, RegionEndpoint.USEast1);

            var fileTransferUtility = new TransferUtility(client);

            try
            {
                await fileTransferUtility.S3Client.PutBucketAsync(new PutBucketRequest()
                {
                    BucketName = "helpet"
                });
            }
            catch { }

            if (!string.IsNullOrWhiteSpace(imageToUpload.Path))
            {
                fileTransferUtility.Upload(new TransferUtilityUploadRequest()
                {
                    FilePath   = imageToUpload.Path,
                    BucketName = imagesBucketName,
                    Key        = $"{imageToUpload.Key ?? imageToUpload.Id}.{imageToUpload.Extension}"
                });
            }

            if (!string.IsNullOrWhiteSpace(imageToUpload.ThumbnailPath))
            {
                fileTransferUtility.Upload(new TransferUtilityUploadRequest()
                {
                    FilePath   = imageToUpload.ThumbnailPath,
                    BucketName = thumbnailsBucketName,
                    Key        = $"{imageToUpload.Key ?? imageToUpload.Id}.{imageToUpload.Extension}"
                });
            }

            fileTransferUtility.Dispose();

            client.Dispose();
        }
Beispiel #12
0
        public async Task <AddFileResponse> UploadFile(string bucketName, string key, IFormFile file)
        {
            if (string.IsNullOrEmpty(key))
            {
                key = file.FileName;
            }
            else if (!key.EndsWith(Path.GetExtension(file.FileName)))
            {
                key = string.Join("", key, Path.GetExtension(file.FileName));
            }

            var response = new List <string>();

            var uploadRequest = new TransferUtilityUploadRequest
            {
                InputStream = file.OpenReadStream(),
                Key         = key,
                BucketName  = bucketName,
                CannedACL   = S3CannedACL.Private
            };

            using (var fileTransferUtility = new TransferUtility(_s3Client))
            {
                await fileTransferUtility.UploadAsync(uploadRequest);
            }

            var expiryUriRequest = new GetPreSignedUrlRequest
            {
                BucketName = bucketName,
                Key        = key,
                Expires    = DateTime.Now.AddDays(1)
            };

            var url = _s3Client.GetPreSignedURL(expiryUriRequest);

            response.Add(url);

            return(new AddFileResponse
            {
                PreSignedUrl = response
            });
        }
Beispiel #13
0
        private bool GetRestorePoint(Amazon.S3.Util.S3EventNotification.S3Entity s3Event)
        {
            var attempt = 0;

            while (true)
            {
                try
                {
                    attempt++;
                    var msg   = new StringBuilder();
                    var timer = new Stopwatch();
                    timer.Start();
                    using (var transferUtility = new TransferUtility(this.S3Client))
                        using (var responseStream = transferUtility.OpenStream(s3Event.Bucket.Name, s3Event.Object.Key))
                            using (var reader = new StreamReader(responseStream))
                            {
                                string line;
                                while ((line = reader.ReadLine()) != null)
                                {
                                    var fileName = line.Split(':')[0];
                                    var rowIndex = long.Parse(line.Split(':')[1]);
                                    _restorePoint.Add(fileName, rowIndex);
                                    msg.Append($"{fileName}:{rowIndex};");
                                }
                            }

                    timer.Stop();
                    Console.WriteLine("Restore point:" + msg + " | " + timer.ElapsedMilliseconds + "ms");
                    return(true);
                }
                catch (Exception e)
                {
                    if (attempt > 5)
                    {
                        Console.WriteLine($"WARN_EXC - GetRestorePoint [{s3Event.Object.Key}]");
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.StackTrace);
                        throw;
                    }
                }
            }
        }
        public static async Task UploadFileToS3(string awskeyId, string awskey,
                                                string bucketname, string key, string info)
        {
            using (var client = string.IsNullOrEmpty(awskeyId) ? new AmazonS3Client(RegionEndpoint.USEast1) : new AmazonS3Client(awskeyId, awskey, RegionEndpoint.USEast1))
            {
                using (var newMemoryStream = new MemoryStream())
                {
                    var uploadRequest = new TransferUtilityUploadRequest
                    {
                        InputStream = new MemoryStream(Encoding.UTF8.GetBytes(info)),
                        Key         = key,
                        BucketName  = bucketname,
                        CannedACL   = S3CannedACL.PublicRead
                    };

                    var fileTransferUtility = new TransferUtility(client);
                    await fileTransferUtility.UploadAsync(uploadRequest);
                }
            }
        }
Beispiel #15
0
        /// <summary>
        ///     Upload file to S3 bucket
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="keyName"></param>
        /// <param name="bucketName"></param>
        /// <param name="bucketRegion"></param>
        /// <returns></returns>
        public async Task Upload(string filePath, string keyName, string bucketName, RegionEndpoint bucketRegion)
        {
            var s3Client = new AmazonS3Client(bucketRegion);

            var fileTransferUtility        = new TransferUtility(s3Client);
            var fileTransferUtilityRequest = new TransferUtilityUploadRequest
            {
                BucketName   = bucketName,
                FilePath     = filePath,
                StorageClass = S3StorageClass.StandardInfrequentAccess,
                PartSize     = 6291456, // 6 MB.
                Key          = keyName,
                CannedACL    = S3CannedACL.PublicRead
            };

            //Provide additional meta data
            fileTransferUtilityRequest.Metadata.Add("Source", "aws-textract-demo");

            await fileTransferUtility.UploadAsync(fileTransferUtilityRequest);
        }
Beispiel #16
0
        public async Task UploadFileAsync(string bucketName, string keyName, string filePath)
        {
            try
            {
                var fileTransferUtility =
                    new TransferUtility(s3Client);

                await fileTransferUtility.UploadAsync(filePath, bucketName, keyName);

                Console.WriteLine("Upload completed");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
Beispiel #17
0
        public static async Task UploadingFileAsync()                                               //Method to upload files to S3 bucket
        {
            try
            {
                Console.WriteLine("Sending image to server ...");
                var fileTransferUtility = new TransferUtility(s3Client);

                await fileTransferUtility.UploadAsync(FilePath, bucketName);

                Console.WriteLine("Upload completed.");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
Beispiel #18
0
        private string bucketName = "missions69";      //папка в интернете (корзина)
        //private string keyName = "mission69.xml";        // название файла в папке в инете
        //private string filePath = "App1.dmissions.xml"; //путь файла для отправления/загрузки
        //private string dfilePath = "App1.missions.xml";



        public async void DownloadFile(string path, string key)
        {
            AmazonS3Config config = new AmazonS3Config();

            //config.ServiceURL = "https://missions69.s3.eu-west-3.amazonaws.com/missions.xml" ;
            config.UseHttp        = true;
            config.RegionEndpoint = Amazon.RegionEndpoint.EUWest3;

            var client = new AmazonS3Client("AKIAJO3YO2ATRVV5UQYA", "4+SelI41UmF0oO8IiXazTizmLy60C82Eaem2nonH", config);

            Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), path);
            TransferUtility transferUtility        = new TransferUtility(client);
            TransferUtilityDownloadRequest request = new TransferUtilityDownloadRequest();

            request.Key        = key;
            request.BucketName = bucketName;
            request.FilePath   = path;
            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            await transferUtility.DownloadAsync(request, cancellationToken);
        }
Beispiel #19
0
        public void CreateS3Directory(string bucket, string key, Action <string, string> logger = null)
        {
            if (transferUtil == null)
            {
                transferUtil = new TransferUtility(client);
            }

            if (!key.EndsWith("/"))
            {
                key = key + "/";
            }

            key = key.Replace('\\', '/');

            transferUtil.Upload(Stream.Null, bucket, key);
            if (logger != null)
            {
                logger(bucket, $"Created Directory [s3://{bucket}/{key}].");
            }
        }
Beispiel #20
0
        // s3 upload directory
        private static async Task UploadDirAsync(IAmazonS3 s3Client, string localDirectory, string s3Path)
        {
            try
            {
                // create the directoryTransferUtility object
                var directoryTransferUtility = new TransferUtility(s3Client);

                // Upload a directory
                //await directoryTransferUtility.UploadDirectoryAsync(localDirectory, s3Path, wildCard, SearchOption.AllDirectories);
                await directoryTransferUtility.UploadDirectoryAsync(localDirectory, s3Path);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
Beispiel #21
0
        public async Task <string> StoreFileAsync(FileInfo file)
        {
            // Create Bucket if not created already
            await CreateBucketAsync(_bucketName);

            var fileTransferUtility = new TransferUtility(_client);

            // Upload file with the name it has (NOT RECOMMENDED)
            await fileTransferUtility.UploadAsync(file.FullName, _bucketName);

            // Upload file with our custom name
            var newFileName = Guid.NewGuid().ToString();

            newFileName = $"{newFileName}{file.Extension}";
            await fileTransferUtility.UploadAsync(file.FullName, _bucketName, newFileName);

            var imageUrl = $"https://{_bucketName}.s3.{_regionName}.amazonaws.com/{newFileName}";

            return(imageUrl);
        }
Beispiel #22
0
 public void StartAmazonMain()
 {
     try
     {
         s3Client = new AmazonS3Client(_AccessKeyId, _SecretAccessKey, bucketRegion);
         var fileTransferUtility = new TransferUtility(s3Client);
         UploadFileWithFileNameAsync(fileTransferUtility).Wait();
         UploadObjectKeyNameValueExplicitlyAsync(fileTransferUtility).Wait();
         UploadDataFromIOStreamAsync(fileTransferUtility).Wait();
         UploadDataWithAdvancedSetting(fileTransferUtility).Wait();
     }
     catch (AmazonS3Exception e)
     {
         Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
     }
     catch (Exception e)
     {
         Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
     }
 }
Beispiel #23
0
        public async Task StreamCsvToS3Async(IEnumerable <MediaDetails> medias, string fileKey)
        {
            var output = new MemoryStream();

            using (var writer = new StreamWriter(output))
            {
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    await csv.WriteRecordsAsync(medias);

                    await csv.FlushAsync();

                    using (var transfer = new TransferUtility(Client))
                    {
                        output.Position = 0;
                        await transfer.UploadAsync(output, Bucket, fileKey);
                    }
                }
            }
        }
Beispiel #24
0
        // static AWS asynchronous upload function:
        private static async Task UploadFileAsync()
        {
            try
            {
                var fileTransferUtility = new TransferUtility(s3Client);

                // Option 2. Specify object key name explicitly:
                await fileTransferUtility.UploadAsync(filePath, bucketName, keyName);

                Console.WriteLine("AWS file upload:\t\t\t\t" + "Completed.");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
Beispiel #25
0
 public override bool Write(string fileName, byte[] data)
 {
     try
     {
         if ((!FileExists(fileName)) || Overwrite)
         {
             using (TransferUtility transferUtility = new TransferUtility(AWSClient))
             {
                 using (MemoryStream ms = new MemoryStream(data)) {
                     transferUtility.Upload(ms, BucketName, AccessKey);
                 }
             }
         }
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Beispiel #26
0
        private static void uploadtoS3(Stream fileToUpload, ILogger log, string filename)
        {
            var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(s3Key, s3Secret);

            s3Client = new AmazonS3Client(awsCredentials, bucketRegion);
            try
            {
                var fileTransferUtility = new TransferUtility(s3Client);
                fileTransferUtility.UploadAsync(fileToUpload, bucketName, filename).Wait();
                log.LogInformation("Upload 3 completed");
            }
            catch (AmazonS3Exception e)
            {
                log.LogInformation("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                log.LogInformation("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
Beispiel #27
0
 public string UploadFile(IFormFile formFile)
 {
     try
     {
         var awsConfig           = (AwsSettings)_serviceProvider.GetService(typeof(AwsSettings));
         var fileTransferUtility = new TransferUtility(new AmazonS3Client(awsConfig.AwsS3AccessKey, awsConfig.AwsS3SecretAccessKey, RegionEndpoint.USWest2));
         var imageName           = Guid.NewGuid().ToString();
         imageName = imageName + Path.GetExtension(formFile.FileName);
         using (var stream = formFile.OpenReadStream())
         {
             fileTransferUtility.UploadAsync(stream, awsConfig.AwsS3BucketName, imageName).Wait();
         }
         return(imageName);
     }
     catch (Exception up)
     {
         _logger.LogInformation(up.Message);
         return(null);
     }
 }
        //Uploads file to S3
        private static async Task UploadFileAsync(string filePath, string exportlocation)
        {
            try
            {
                var fileTransferUtility = new TransferUtility(s3Client);

                // Option 1. Upload a file. The file name is used as the object key name.
                await fileTransferUtility.UploadAsync(filePath, string.Format(@"{0}/{1}", BUCKET_NAME, exportlocation));

                Console.WriteLine("Upload 1 completed");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
Beispiel #29
0
        public bool sendMyFileToS3(System.IO.Stream localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
        {
            IAmazonS3       client  = new AmazonS3Client();
            TransferUtility utility = new TransferUtility(client);
            TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();

            if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
            {
                request.BucketName = bucketName; //no subdirectory just bucket name
            }
            else
            {   // subdirectory and bucket name
                request.BucketName = bucketName + @"/" + subDirectoryInBucket;
            }
            request.Key         = fileNameInS3; //file name up in S3
            request.InputStream = localFilePath;
            utility.Upload(request);            //commensing the transfer

            return(true);                       //indicate that the file was sent
        }
        static void ReadObjectData(string keyName, string outputPath)
        {
            try
            {
                var fileTransferUtility =
                    new TransferUtility(getS3Client());

                // Option 2. Specify object key name explicitly.
                fileTransferUtility.Download(outputPath, bucketName, keyName);
                Console.WriteLine("Downlaod completed");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
 internal UploadDirectoryCommand(TransferUtility utility, TransferUtilityUploadDirectoryRequest request)
 {
     this._utility = utility;
     this._request = request;
 }