public static AmazonS3GetAssetResult GetAssetFromS3(string bucketName, string fileNameAndPath)
            {
                try
                {
                    if (String.IsNullOrEmpty(fileNameAndPath))
                    {
                        throw new ArgumentNullException(nameof(fileNameAndPath));
                    }
                    if (String.IsNullOrEmpty(bucketName))
                    {
                        throw new ArgumentNullException(nameof(bucketName));
                    }

                    var client = new AmazonS3Client(new AmazonS3Config()
                    {
                        RegionEndpoint        = RegionEndpoint.APSouth1,
                        UseAccelerateEndpoint = true
                    });

                    GetObjectRequest request = new GetObjectRequest
                    {
                        BucketName = bucketName,
                        Key        = fileNameAndPath
                    };
                    GetObjectResponse response = client.GetObjectAsync(request).Result;

                    if (response.HttpStatusCode.Equals(HttpStatusCode.OK))
                    {
                        Stream       responseStream = response.ResponseStream;
                        var          byteData       = StreamHelper.ReadFully(responseStream);
                        AmazonS3File file           = new AmazonS3File
                        {
                            Headers     = AWSS3Helper.ToNameValueCollection(response.Headers),
                            Content     = byteData,
                            ContentType = response.Headers.ContentType
                        };
                        return(new AmazonS3GetAssetResult {
                            IsSuccess = true, File = file
                        });
                    }
                    else
                    {
                        return(new AmazonS3GetAssetResult {
                            IsSuccess = false, Message = $"Error getting the file, statuscode : {response.HttpStatusCode} and AWSRequestId : {response.ResponseMetadata?.RequestId}"
                        });
                    }
                }
                catch (Exception ex)
                {
                    return(new AmazonS3GetAssetResult {
                        IsSuccess = false, Message = $"Error getting file from s3"
                    });
                }
            }
Ejemplo n.º 2
0
        public IFile GetFile(string path)
        {
            var now = new DateTime(DateTime.Now.Year, 7, 7);

            var file = new AmazonS3File
            {
                Path         = path,
                LastModified = now,
            };

            if (_files.ContainsKey(path))
            {
                file.Size = _files[path].Length;
            }

            return(file);
        }