Пример #1
0
 public async Task <Stream> GetPhotoAsync(string key, string folder)
 {
     using (var client = new AmazonS3Client(_accessKey, _secretKey, RegionEndpoint.USEast1))
     {
         var fileTransferUtility = new TransferUtility(client);
         return(await fileTransferUtility.OpenStreamAsync($"{_bucket}/{folder}", key));
     }
 }
Пример #2
0
        public async Task <byte[]> GetFile(string path)
        {
            using (var stream = await _transferUtility.OpenStreamAsync(_appSettings.Value.BucketName, path))
                using (var ms = new MemoryStream()) {
                    await stream.CopyToAsync(ms);

                    return(ms.ToArray());
                }
        }
Пример #3
0
 public async Task <Stream> GetFileStreamAsync(string path)
 {
     try
     {
         var transferUtility = new TransferUtility(_s3Client);
         return(await transferUtility.OpenStreamAsync(_s3Settings.S3BucketName, GetCompletePath(path)));
     }
     catch (Exception)
     {
         throw new FileStoreException($"Cannot get file stream because the file '{path}' does not exist.");
     }
 }
Пример #4
0
 public async Task <Stream> GetFileStreamAsync(string path)
 {
     try
     {
         var transferUtility = new TransferUtility(_amazonS3Client);
         return(await transferUtility.OpenStreamAsync(_options.BucketName, this.Combine(_basePrefix, path)));
     }
     catch (AmazonS3Exception)
     {
         throw new FileStoreException($"Cannot get file stream because the file '{path}' does not exist.");
     }
 }
        /// <summary>
        /// Use the Amazon S3 TransferUtility to retrieve the text to translate
        /// from an object in an Amazon S3 bucket.
        /// </summary>
        /// <param name="srcBucket">The name of the Amazon S3 bucket where the
        /// text is stored.
        /// </param>
        /// <param name="srcTextFile">The key of the Amazon S3 object that
        /// contains the text to translate.</param>
        /// <returns>A string representing the source text.</returns>
        public static async Task <string> GetSourceTextAsync(string srcBucket, string srcTextFile)
        {
            string srcText = string.Empty;

            var             s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2);
            TransferUtility utility  = new TransferUtility(s3Client);

            using var stream = await utility.OpenStreamAsync(srcBucket, srcTextFile);

            StreamReader file = new System.IO.StreamReader(stream);

            srcText = file.ReadToEnd();
            return(srcText);
        }
Пример #6
0
 async Task ReadingAnObjectFromS3AsAStream()
 {
     await CarryOutAWSTask(async() =>
     {
         var fileTransferUtility = new TransferUtility(client);
         using (var fs = await fileTransferUtility.OpenStreamAsync(bucketName, fileName, CancellationToken.None))
         {
             using (var reader = new StreamReader(fs))
             {
                 var contents = await reader.ReadToEndAsync();
                 Console.WriteLine($"Content of file {fileName} is");
                 Console.WriteLine(contents);
             }
         }
     }, "Reading an Object from S3 as a Stream");
 }
Пример #7
0
        /// <inheritdoc />
        public async Task <Stream> OpenReadAsync(
            IUnixFileEntry fileEntry,
            long startPosition,
            CancellationToken cancellationToken)
        {
            var stream = await _transferUtility.OpenStreamAsync(
                _options.BucketName,
                ((S3FileSystemEntry)fileEntry).Key,
                cancellationToken);

            if (startPosition != 0)
            {
                stream.Seek(startPosition, SeekOrigin.Begin);
            }

            return(stream);
        }
Пример #8
0
        public async Task <Stream> StreamFileDownload(string path, string bucket = null)
        {
            var finalBucket = bucket ?? DefaultBucket;

            if (string.IsNullOrWhiteSpace(finalBucket))
            {
                throw new Exception("S3 Bucket Is Required");
            }
            using var utility = new TransferUtility(s3Client);
            var request = new TransferUtilityOpenStreamRequest
            {
                BucketName = finalBucket,
                Key        = path
            };

            return(await utility.OpenStreamAsync(request));
        }
Пример #9
0
        public async Task <Response <Base64ResponseMessage> > GetBase64Async(Base64RequestMessage requestMessage)
        {
            var response = Response <Base64ResponseMessage> .Create();

            if (response.WithMessages(ValidateRequest(requestMessage).Messages).HasError)
            {
                return(response);
            }

            using (var stream = await TransferUtility.OpenStreamAsync(requestMessage.BucketName, $"{requestMessage.FolderName}/{requestMessage.FileName}"))
            {
                byte[] bytes;

                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    bytes = memoryStream.ToArray();
                }

                var images = new List <string> {
                    "jpg", "jpeg", "gif", "png", "bmp"
                };

                if (requestMessage.FileExtension.Contains("pdf"))
                {
                    response.Data.Value.Base64 = $"data:application/pdf;base64,{Convert.ToBase64String(bytes)}";
                }
                else if (images.Contains(requestMessage.FileExtension.Replace(".", "")))
                {
                    response.Data.Value.Base64 = $"data:image/{requestMessage.FileExtension.Replace(".", "")};base64,{Convert.ToBase64String(bytes)}";
                }
                else
                {
                    response.Data.Value.Base64 = $"data:application/octet-stream;base64,{Convert.ToBase64String(bytes)}";
                }
            }

            response.Data.Value.BucketName    = requestMessage.BucketName;
            response.Data.Value.FolderName    = requestMessage.FolderName;
            response.Data.Value.FileName      = requestMessage.FileName;
            response.Data.Value.FileExtension = requestMessage.FileExtension;
            response.Data.Value.Link          = $"{Options.Value.StorageAws.BaseUrl}/{requestMessage.FolderName}/{requestMessage.FileName}";

            return(response);
        }
Пример #10
0
        /// <summary>
        /// Reads the config file from S3
        /// </summary>
        /// <returns>The health check request configuration</returns>
        private static async Task <HealthCheckConfiguration> ReadConfig(AreWeUpClientConfig defaultConfig)
        {
            try
            {
                //Stream the content of the S3 object
                using (Stream Result = await _XFerUtil.OpenStreamAsync(new TransferUtilityOpenStreamRequest()
                {
                    BucketName = defaultConfig.S3Bucket, Key = defaultConfig.S3Key
                }))
                {
                    //Read the stream
                    using (StreamReader Reader = new StreamReader(Result))
                    {
                        JObject JO        = JObject.Parse(await Reader.ReadToEndAsync());
                        JObject NewConfig = new JObject();

                        // Iterates the keys like Http, Https, Tcp, Udp, Icmp
                        foreach (KeyValuePair <string, JToken> Item in JO)
                        {
                            JArray ItemArray = new JArray();

                            // Iterates each config in that category
                            foreach (JObject Config in ((JArray)JO[Item.Key]).Children <JObject>())
                            {
                                // Add in the default values if they weren't defined.
                                JToken CId = Config.GetValue("CustomerId", StringComparison.OrdinalIgnoreCase);

                                if (CId == null)
                                {
                                    if (!String.IsNullOrEmpty(defaultConfig.DefaultCustomerId))
                                    {
                                        Config.Add("CustomerId", defaultConfig.DefaultCustomerId);
                                    }
                                }

                                JToken CW = Config.GetValue("SendToCloudWatch", StringComparison.OrdinalIgnoreCase);

                                if (CW == null)
                                {
                                    Config.Add("SendToCloudWatch", defaultConfig.DefaultSendToCloudWatch);
                                }

                                if (Item.Key == "Https")
                                {
                                    JToken SSL = Config.GetValue("IgnoreSslErrors", StringComparison.OrdinalIgnoreCase);

                                    if (SSL == null)
                                    {
                                        Config.Add("IgnoreSslErrors", defaultConfig.DefaultIgnoreSslErrors);
                                    }
                                }

                                if (Item.Key.Equals("Tcp", StringComparison.OrdinalIgnoreCase) ||
                                    Item.Key.Equals("Udp", StringComparison.OrdinalIgnoreCase) ||
                                    Item.Key.Equals("Icmp", StringComparison.OrdinalIgnoreCase))
                                {
                                    JToken Timeout = Config.GetValue("Timeout", StringComparison.OrdinalIgnoreCase);

                                    if (Timeout == null)
                                    {
                                        Config.Add("Timeout", defaultConfig.DefaultTimeout);
                                    }
                                }
                                else
                                {
                                    // Otherwise this is a web request, which will all use the
                                    // same request timeout value
                                    Config.Add("Timeout", defaultConfig.HttpRequestTimeout);
                                }

                                JToken SNS = Config.GetValue("SNSTopicARN", StringComparison.OrdinalIgnoreCase);

                                if (SNS == null)
                                {
                                    Config.Add("SNSTopicArn", defaultConfig.SNSTopicArn);
                                }

                                JToken Subject = Config.GetValue("Subject", StringComparison.OrdinalIgnoreCase);

                                if (Subject == null)
                                {
                                    if (!String.IsNullOrEmpty(defaultConfig.DefaultSubject))
                                    {
                                        Config.Add("Subject", defaultConfig.DefaultSubject);
                                    }
                                }

                                ItemArray.Add(Config);
                            }

                            NewConfig.Add(Item.Key, ItemArray);
                        }

                        JsonSerializer Serializer = new JsonSerializer();

                        // Be able to convert the string method value to an HttpMethod class object
                        Serializer.Converters.Add(new HttpMethodConverter());
                        Serializer.Converters.Add(new KeyValueConverter());

                        // Be able to set properties that have a private setter
                        Serializer.ContractResolver = new PrivateSetterResolver();

                        return(NewConfig.ToObject <HealthCheckConfiguration>(Serializer));
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error processing the S3 object { defaultConfig.S3Bucket}\\{ defaultConfig.S3Key}:\r\n{SerializeException(e)}");

                return(null);
            }
        }