Пример #1
0
        /// <summary>
        /// Download a single asset.
        /// </summary>
        /// <param name="asset">The asset to download.</param>
        /// <param name="destination">Where to save the asset.</param>
        /// <param name="progress">An IProgress object to report download activities.</param>
        /// <param name="update">Redownload and overwrite an existing asset of the same name.</param>
        public async Task DownloadAssetAsync(Asset asset, string destination, EventHandler <WriteObjectProgressArgs> progress = null, bool update = false)
        {
            if (client == null)
            {
                throw new Exception("You must authenticate first.");
            }
            if (File.Exists(destination) && !update)
            {
                return;
            }

            string categoryAndName = $"{asset.assetCategory.Value}/{asset.AssetName}";

            var downloadRequest = new TransferUtilityDownloadRequest()
            {
                BucketName = await GetBucketNameAsync(categoryAndName).ConfigureAwait(false),
                Key        = categoryAndName,
                FilePath   = destination
            };

            downloadRequest.WriteObjectProgressEvent += progress;
            await transfer.DownloadAsync(downloadRequest).ConfigureAwait(false);

            downloadRequest.WriteObjectProgressEvent -= progress;
        }
Пример #2
0
        // downlaod a single file to " Resources/Images/ "
        public static void DownloadFile(string fileName)
        {
            if (!Directory.Exists("Resources/Images"))
            {
                Directory.CreateDirectory("Resources/Images");
            }
            string filePath = $"Resources/Images/{fileName}";

            using (s3Client = new AmazonS3Client(bucketRegion))
            {
                DownloadFileAsync().Wait();
            }

            async Task DownloadFileAsync()
            {
                try
                {
                    var fileTransferUtility = new TransferUtility(s3Client);

                    await fileTransferUtility.DownloadAsync(filePath, bucketName, fileName).ConfigureAwait(false);

                    Console.WriteLine("download finished");
                }
                catch (AmazonS3Exception e)
                {
                    Console.WriteLine("AmazonS3Exception: " + e);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e);
                }
            }
        }
Пример #3
0
 async Task DownloadingAnObjectFromS3AsAStream()
 {
     await CarryOutAWSTask(async() =>
     {
         var fileTransferUtility = new TransferUtility(client);
         string theTempFile      = Path.Combine(Path.GetTempPath(), "SavedS3TextFile.txt");
         try
         {
             await fileTransferUtility.DownloadAsync(theTempFile, bucketName, fileName);
             using (var fs = new FileStream(theTempFile, FileMode.Open))
             {
                 using (var reader = new StreamReader(fs))
                 {
                     var contents = await reader.ReadToEndAsync();
                     Console.WriteLine($"Content of saved file {theTempFile} is");
                     Console.WriteLine(contents);
                 }
             }
         }
         finally
         {
             File.Delete(theTempFile);
         }
     }, "Downloading an Object from S3 as a Stream");
 }
Пример #4
0
        private async Task <bool> ProcessZipFileItemsAsync(string bucketName, string key, Func <ZipArchiveEntry, Task> callbackAsync)
        {
            var tmpFilename = Path.GetTempFileName() + ".zip";

            try {
                _logger.LogInfo($"downloading s3://{bucketName}/{key}");
                await _transferUtility.DownloadAsync(new TransferUtilityDownloadRequest {
                    BucketName = bucketName,
                    Key        = key,
                    FilePath   = tmpFilename
                });
            } catch (Exception e) {
                _logger.LogErrorAsWarning(e, "s3 download failed");
                return(false);
            }
            try {
                using (var zip = ZipFile.Open(tmpFilename, ZipArchiveMode.Read)) {
                    foreach (var entry in zip.Entries)
                    {
                        await callbackAsync(entry);
                    }
                }
            } finally {
                try {
                    File.Delete(tmpFilename);
                } catch { }
            }
            return(true);
        }
Пример #5
0
        public async Task <IActionResult> Get(string tag)
        {
            var client = new AmazonS3Client(key, secret, new AmazonS3Config
            {
                ServiceURL = url
            });

            var objs = await GetObjects();

            var spaceObject = objs.FirstOrDefault(x => x.ETag.Equals($"{tag}"));

            if (spaceObject == null)
            {
                return(BadRequest());
            }

            var transferUtility = new TransferUtility(client);

            var fileName  = spaceObject.Key.Split("/", StringSplitOptions.RemoveEmptyEntries).Last();
            var directory = Directory.GetCurrentDirectory();
            var filePath  = $@"{directory}\temporary\{fileName}";

            if (!System.IO.File.Exists(filePath))
            {
                await transferUtility.DownloadAsync(new TransferUtilityDownloadRequest
                {
                    BucketName = bucket,
                    Key        = spaceObject.Key,
                    FilePath   = filePath
                });
            }

            return(PhysicalFile(filePath, filePath.GetMimeType(), fileName));
        }
Пример #6
0
        private async Task ProcessZipFileEntriesAsync(string bucketName, string key, Func <ZipArchiveEntry, Task> callbackAsync)
        {
            var tmpFilename = Path.GetTempFileName() + ".zip";

            try {
                await _transferUtility.DownloadAsync(new TransferUtilityDownloadRequest {
                    BucketName = bucketName,
                    Key        = key,
                    FilePath   = tmpFilename
                });
            } catch {
                LogWarn($"unable to dowload zip file from s3://{bucketName}/{key}");
                return;
            }
            try {
                using (var zip = ZipFile.Open(tmpFilename, ZipArchiveMode.Read)) {
                    foreach (var entry in zip.Entries)
                    {
                        await callbackAsync(entry);
                    }
                }
            } finally {
                try {
                    File.Delete(tmpFilename);
                } catch { }
            }
        }
Пример #7
0
        public async Task <IActionResult> GetMovieAsync(int?id)
        {
            var movie = await _context.Movie
                        .FirstOrDefaultAsync(m => m.Id == id);

            string FileName = movie.Title;

            try
            {
                Debug.WriteLine("In the try creating instance to download");
                var fileTransferUtility = new TransferUtility(s3);
                // Option 1
                // await fileTransferUtility.UploadAsync(sourcePath, bucketName);
                // Debug.WriteLine("Upload 1 Complete");

                Debug.WriteLine(FileName);
                // Option 2
                Debug.WriteLine("Before download");
                await fileTransferUtility.DownloadAsync("C:\\Users\\" + Environment.ExpandEnvironmentVariables("%USERNAME%") + "\\Downloads\\" + FileName, bucketName, FileName);

                Debug.WriteLine("Download 2 Complete");
            }
            catch (AmazonS3Exception e)
            {
                Debug.WriteLine("Error encountered in the server" + e.Message);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Unknown error encountered in the server." + e.Message);
            }
            ViewData["DownloadComplete"] = "Download complete!";
            return(View("Index", await _context.Movie.Include(x => x.UserMovie).ToListAsync()));
        }
Пример #8
0
        private static async Task DownloadAsync(string bucketName, string keyName, string filePath)
        {
            try
            {
                var fileTransferUtility = new TransferUtility(_s3Client);

                var downloadRequest =
                    new TransferUtilityDownloadRequest
                {
                    BucketName = bucketName,
                    FilePath   = filePath,
                    Key        = keyName
                };

                downloadRequest.WriteObjectProgressEvent += new EventHandler <WriteObjectProgressArgs>(DownloadPartProgressEvent);
                await fileTransferUtility.DownloadAsync(downloadRequest);
            }
            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);
            }
        }
Пример #9
0
        private static async Task <bool> DownloadFileAsync(IAmazonS3 amazonS3Client, string filePath)
        {
            try
            {
                var fileTransferUtility = new TransferUtility(amazonS3Client);

                // Download Object Using Key Name
                await fileTransferUtility.DownloadAsync(filePath, BucketName, KeyName);

                Console.WriteLine("Download Completed");

                return(true);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);

                return(false);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);

                return(false);
            }
        }
Пример #10
0
        private async static Task DownloadFile()
        {
            using var s3Client = new AmazonS3Client(RegionEndpoint.USEast1);

            var transfer = new TransferUtility(s3Client);

            var localPath = "C:/teste/file-s3.jpg";

            await transfer.DownloadAsync(localPath, "edmar-bucket-test", "image.jpg");
        }
Пример #11
0
        public async Task <T> DownloadJson <T>(string bucketName, string objectKey)
        {
            var path = GetTmpFile();
            await _fileTransferUtility.DownloadAsync(path, bucketName, objectKey);

            var json = await File.ReadAllTextAsync(path);

            File.Delete(path);
            return(JsonConvert.DeserializeObject <T>(json));
        }
Пример #12
0
 protected override async Task DownloadFileCore(string id, string file)
 {
     var fileTransferUtility = new TransferUtility(_s3Client);
     await fileTransferUtility.DownloadAsync(new TransferUtilityDownloadRequest
     {
         Key        = id,
         BucketName = _bucket,
         FilePath   = file,
     });
 }
Пример #13
0
        public static async Task <T?> DownloadJsonAsync <T>(this AmazonS3Client client, string objectKey, string bucket)
        {
            using var fileTransferUtility = new TransferUtility(client);
            var path = GetTmpFile();
            await fileTransferUtility.DownloadAsync(path, bucket, objectKey);

            var json = await File.ReadAllTextAsync(path);

            File.Delete(path);
            return(JsonSerializer.Deserialize <T>(json));
        }
Пример #14
0
        public async Task <StorageClientDownloadResponse> Download(StorageClientDownloadRequest request)
        {
            AmazonS3Client s3Client = new AmazonS3Client(accesskey, secretkey, bucketRegion);

            TransferUtility fileTransferUtility = new TransferUtility(s3Client);

            try
            {
                string tempFileLocation = Path.GetTempFileName();

                TransferUtilityDownloadRequest fileTransferUtilityRequest = new TransferUtilityDownloadRequest
                {
                    BucketName = bucketName,
                    FilePath   = tempFileLocation,
                    Key        = request.DownloadPath
                };
                await fileTransferUtility.DownloadAsync(fileTransferUtilityRequest);

                fileTransferUtility.Dispose();

                byte[] fileData = await File.ReadAllBytesAsync(tempFileLocation);

                //try clean up
                try
                {
                    File.Delete(tempFileLocation);
                }
                catch {
                    //not the worse if it fails...
                }
                return(new StorageClientDownloadResponse {
                    FilePath = request.DownloadPath, FileData = fileData
                });
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                     ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    return(new StorageClientDownloadResponse {
                        Success = false, Message = "Check the provided AWS Credentials."
                    });
                }
                else
                {
                    return(new StorageClientDownloadResponse {
                        Success = false, Message = $"Error occurred: {amazonS3Exception.Message}"
                    });
                }
            }
        }
Пример #15
0
        protected void TransferUtilityDownload(string bucket, string prefix, string downloadpath)
        {
            TransferUtility ftu = new TransferUtility(Authentication.Client);

            TransferUtilityDownloadRequest request = new TransferUtilityDownloadRequest
            {
                BucketName = bucket,
                Key        = prefix,
                FilePath   = downloadpath
            };

            ftu.DownloadAsync(request).Wait();
        }
Пример #16
0
        public async Task DownloadFile(string bucketName, string fileName)
        {
            var pathAndFileName = $"/Users/sophoap/tmp/s3temp/{fileName}";
            var downloadRequest = new TransferUtilityDownloadRequest
            {
                BucketName = bucketName,
                Key        = fileName,
                FilePath   = pathAndFileName
            };

            using var transferUtility = new TransferUtility(_s3Client);
            await transferUtility.DownloadAsync(downloadRequest);
        }
Пример #17
0
        public async Task DownloadAsync(string bucketName, string fileName)
        {
            var downloadPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}/Downloads/s3Temp/{fileName}";
            var request      = new TransferUtilityDownloadRequest {
                BucketName = bucketName,
                Key        = fileName,
                FilePath   = downloadPath,
            };

            using (var transferUtility = new TransferUtility(_s3Client)) {
                await transferUtility.DownloadAsync(request);
            }
        }
Пример #18
0
        public async Task GetImageAsync(string bucketName, string objectKey, string filePath)
        {
            using (var transferUtility = new TransferUtility(_s3Client, _config))
            {
                var request = new TransferUtilityDownloadRequest
                {
                    BucketName = bucketName,
                    Key        = objectKey,
                    FilePath   = filePath
                };

                await transferUtility.DownloadAsync(request);
            }
        }
Пример #19
0
        public async Task DownloadFile(string bucketName, string fileName)
        {
            var pathAndFileName = $"C:\\S3Temp\\{fileName}";
            var downloadRequest = new TransferUtilityDownloadRequest
            {
                BucketName = bucketName,
                Key        = fileName,
                FilePath   = pathAndFileName
            };

            using (var transferUtility = new TransferUtility(_s3Client))
            {
                await transferUtility.DownloadAsync(downloadRequest);
            }
        }
Пример #20
0
        public async Task Should_TransferUtitlity()
        {
            var utility = new TransferUtility(client);

            var request = new TransferUtilityDownloadRequest
            {
                BucketName = "ecap-falcon",
                Key        = "03CB552E-A3FE-4EF9-A600-909FBA63E900/000c4fcd95e34d90958a5605f2976b6e/461d8d9c-cd33-425e-bb11-3f78b5f98089/000c4fcd95e34d90958a5605f2976b6e.png",
                FilePath   = @"d:\abcd.png",
            };

            await utility.DownloadAsync(request);

            //client.CopyObjectAsync();
        }
Пример #21
0
        public async Task TestServerlessPackage()
        {
            var logger   = new TestToolLogger();
            var assembly = this.GetType().GetTypeInfo().Assembly;

            var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + "../../../../../../testapps/TestServerlessWebApp");
            var command  = new PackageCICommand(logger, fullPath, new string[0]);

            command.Region                       = "us-west-2";
            command.Configuration                = "Release";
            command.CloudFormationTemplate       = "serverless-arm.template";
            command.CloudFormationOutputTemplate = Path.Combine(Path.GetTempPath(), "output-serverless-arm.template");
            command.S3Bucket                     = "serverless-package-test-" + DateTime.Now.Ticks;
            command.DisableInteractive           = true;

            if (File.Exists(command.CloudFormationOutputTemplate))
            {
                File.Delete(command.CloudFormationOutputTemplate);
            }


            await command.S3Client.PutBucketAsync(command.S3Bucket);

            try
            {
                Assert.True(await command.ExecuteAsync());
                Assert.True(File.Exists(command.CloudFormationOutputTemplate));

                var templateJson = File.ReadAllText(command.CloudFormationOutputTemplate);
                var templateRoot = JsonConvert.DeserializeObject(templateJson) as JObject;
                var codeUri      = templateRoot["Resources"]["DefaultFunction"]["Properties"]["CodeUri"].ToString();
                Assert.False(string.IsNullOrEmpty(codeUri));

                var s3Key = codeUri.Split('/').Last();

                var transfer        = new TransferUtility(command.S3Client);
                var functionZipPath = Path.GetTempFileName();
                await transfer.DownloadAsync(functionZipPath, command.S3Bucket, s3Key);

                Assert.Equal("linux-arm64", GetRuntimeFromBundle(functionZipPath));

                File.Delete(functionZipPath);
            }
            finally
            {
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(command.S3Client, command.S3Bucket);
            }
        }
Пример #22
0
        public async Task DownloadFile(string bucketName, string fileName)
        {
            var pathAndFileName = $"C:\\Users\\magef\\AppData\\Local\\CSReporterV3\\{fileName}";

            var downloadRequest = new TransferUtilityDownloadRequest
            {
                BucketName = bucketName,
                Key        = fileName,
                FilePath   = pathAndFileName
            };

            using (var transferUtility = new TransferUtility(_s3Client))
            {
                await transferUtility.DownloadAsync(downloadRequest);
            };
        }
Пример #23
0
        public async Task DownloadFileAsync(string filePath, string bucketName, string keyName)
        {
            try
            {
                var fileTransferUtility = new TransferUtility(s3Client);

                await fileTransferUtility.DownloadAsync(filePath, bucketName, keyName);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine($"Error encountered on server. Message:'{e.Message}' when reading an object");
            }
            catch (Exception e)
            {
                Console.WriteLine($"Unknown encountered on server. Message:'{e.Message}' when reading an object");
            }
        }
Пример #24
0
        /// <summary>
        /// Download file from s3 to local temp folder
        /// </summary>
        /// <param name="bucketName">bucket name</param>
        /// <param name="key">key = path + file name</param>
        /// <param name="temporalPath">temporal path to download + file name</param>
        /// <returns>task just to download file</returns>
        public async Task DownloadFile(string bucketName, string key, string temporalPath)
        {
            GetObjectRequest request = new GetObjectRequest
            {
                BucketName = bucketName,
                Key        = key
            };

            var downloadRequest = new TransferUtilityDownloadRequest
            {
                BucketName = bucketName,
                Key        = key,
                FilePath   = temporalPath,
            };

            using var transferUtility = new TransferUtility(_s3Client);
            await transferUtility.DownloadAsync(downloadRequest);
        }
Пример #25
0
        /// <summary>
        /// Downloads the S3 object and saves it the specified  destination asynchronously.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="destinationPath">The destination path.</param>
        /// <returns></returns>
        public async Task CopyToLocalAsync(string path, string destinationPath)
        {
            var bucketName = ExtractBucketNameFromPath(path);
            var subPath    = path.SubstringAfterChar(bucketName + "/");

            var client = InitialiseClient();

            var utlity = new TransferUtility(client);

            var request = new TransferUtilityDownloadRequest
            {
                BucketName = bucketName,
                Key        = subPath,
                FilePath   = destinationPath
            };

            await utlity.DownloadAsync(request);
        }
Пример #26
0
        public async Task DownloadFile(string bucketName, string fileName)
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp");

            Directory.CreateDirectory(path);
            var pathAndFileName = Path.Combine(path, fileName);
            var downloadRequest = new TransferUtilityDownloadRequest
            {
                BucketName = bucketName,
                Key        = fileName,
                FilePath   = pathAndFileName,
            };

            using (var transferUtility = new TransferUtility(_s3Client))
            {
                await transferUtility.DownloadAsync(downloadRequest);
            }
        }
Пример #27
0
        static async Task Main()
        {
            var region   = Amazon.RegionEndpoint.APNortheast1;
            var client   = new AmazonS3Client(region);
            var transfer = new TransferUtility(client);

            const string bucketName = "<please input your bucket name>";

            // ファイルのアップロード
            const string uploadFilePath = "s3_sample_upload.txt";
            const string key            = "s3_sample.txt";
            await File.WriteAllTextAsync(uploadFilePath, "Hello S3 !!");

            await transfer.UploadAsync(uploadFilePath, bucketName, key);

            // ファイルのダウンロード
            const string downloadFilePath = "s3_sample_download.txt";
            await transfer.DownloadAsync(downloadFilePath, bucketName, key);
        }
Пример #28
0
        /// <summary>
        /// Processes the job.
        /// </summary>
        /// <param name="job">The job to process.</param>
        /// <returns>An awaitable task.</returns>
        public async Task ProcessAsync(ProcessJob job)
        {
            var    util    = new TransferUtility(job.S3);
            string tmpPath = Path.GetTempFileName();
            await util.DownloadAsync(new TransferUtilityDownloadRequest()
            {
                BucketName = job.SourceBucketName,
                Key        = job.SourceKey,
                FilePath   = tmpPath,
            });

            using var stream        = File.OpenRead(tmpPath);
            using var pgnGameStream = new PgnGameStream(stream);
            while (!pgnGameStream.EndOfStream)
            {
                var nextGame = pgnGameStream.ParseNextGame();
                var(game, rows) = Flattener.FlattenPgnGame(nextGame, job.DatasetId);
            }
        }
Пример #29
0
        public async Task <GetFileResponse> DownloadFile(string bucketName, string fileName)
        {
            var response = new List <KeyValuePair <string, string> >();

            try
            {
                var pathAndFileName = $"C:\\S3Test\\{fileName}";

                var downloadRequest = new TransferUtilityDownloadRequest
                {
                    BucketName = bucketName,
                    Key        = fileName,
                    FilePath   = pathAndFileName
                };

                using (var transferUtility = new TransferUtility(_s3Client))
                {
                    await transferUtility.DownloadAsync(downloadRequest);

                    var metadataRequest = new GetObjectMetadataRequest
                    {
                        BucketName = bucketName,
                        Key        = fileName
                    };

                    var metadata = await _s3Client.GetObjectMetadataAsync(metadataRequest);

                    foreach (string key in metadata.Metadata.Keys)
                    {
                        response.Add(new KeyValuePair <string, string>(key, metadata.Metadata[key]));
                    }

                    return(new GetFileResponse
                    {
                        Metadata = response
                    });
                }
            }
            catch (AmazonS3Exception ex)
            {
                throw ex;
            }
        }
Пример #30
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);
        }