GetObjectAsync() public method

Initiates the asynchronous execution of the GetObject operation.
public GetObjectAsync ( GetObjectRequest request, System cancellationToken = default(CancellationToken) ) : Task
request GetObjectRequest Container for the necessary parameters to execute the GetObject operation.
cancellationToken System /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. ///
return Task
コード例 #1
4
        public async Task UploadDataFileAsync(string data, string storageKeyId, string storageSecret, string region)
        {
            var regionIdentifier = RegionEndpoint.GetBySystemName(region);
            using (var client = new AmazonS3Client(storageKeyId, storageSecret, regionIdentifier))
            {
                try
                {
                    var putRequest1 = new PutObjectRequest
                    {
                        BucketName = AwsBucketName,
                        Key = AwsBucketFileName,
                        ContentBody = data,
                        ContentType = "application/json"
                    };

                    await client.PutObjectAsync(putRequest1);
                }
                catch (AmazonS3Exception amazonS3Exception)
                {
                    if (amazonS3Exception.ErrorCode != null &&
                        (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                    {
                        throw new SecurityException("Invalid Amazon S3 credentials - data was not uploaded.", amazonS3Exception);
                    }

                    throw new HttpRequestException("Unspecified error attempting to upload data: " + amazonS3Exception.Message, amazonS3Exception);
                }

                var response = await client.GetObjectAsync(AwsBucketName, AwsBucketFileName);
                using (var reader = new StreamReader(response.ResponseStream))
                {
                    Debug.WriteLine(await reader.ReadToEndAsync());
                }
            }
        }
コード例 #2
0
ファイル: IStorage.cs プロジェクト: guitarrapc/benchmark-lab
        /// <summary>
        /// Get report from S3 Bucket
        /// </summary>
        /// <param name="path"></param>
        /// <param name="prefix"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task <string[]> Get(string path, string prefix, CancellationToken ct = default)
        {
            _logger?.LogDebug($"downloading content from S3. bucket {path}, prefix {prefix}");

            // todo: continuation
            var objects = await ListObjectsAsync(path, prefix, ct);

            if (!objects.Any())
            {
                return(Array.Empty <string>());
            }

            var contents = new ConcurrentBag <string>();
            var tasks    = objects.Select(async x =>
            {
                using var res = await _client.GetObjectAsync(new Amazon.S3.Model.GetObjectRequest
                {
                    BucketName = x.BucketName,
                    Key        = x.Key,
                }, ct);
                using var stream = res.ResponseStream;
                using var reader = new StreamReader(stream);
                var content      = await reader.ReadToEndAsync();
                contents.Add(content);
            });
            await Task.WhenAll(tasks);

            return(contents.ToArray());
        }
コード例 #3
0
        public byte[] Get(string key)
        {
            Amazon.S3.AmazonS3Client client = getS3Client();
            var resTask = client.GetObjectAsync(bucketName, key);

            try
            {
                resTask.Wait();
            }
            catch (AggregateException ex)
            {
                BatchProcessor.Logging.Logger.Error(ex, "Error retreiving an item from S3 bucket, bucket name=" + bucketName + ", item key=" + key);
                //TODO: Check the first exception, and make sure that it is 404 not found.
                return(null);
            }
            byte[] res;
            using (resTask.Result.ResponseStream)
            {
                using (MemoryStream mem = new MemoryStream())
                {
                    resTask.Result.ResponseStream.CopyTo(mem);
                    res = mem.ToArray();
                }
            }
            return(res);
        }
コード例 #4
0
        /// <summary>
        /// https://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingNetSDK.html
        /// </summary>
        /// <returns></returns>
        async Task ReadObjectDataAsync(string keyName, string fileName)
        {
            string           responseBody = "";
            GetObjectRequest request      = new GetObjectRequest
            {
                BucketName = awsBucket,
                Key        = keyName
            };

            using (GetObjectResponse response = await s3Client.GetObjectAsync(request))
                using (Stream responseStream = response.ResponseStream) {
                    var fileStream = File.Create(fileName);
                    //responseStream.Seek(0, SeekOrigin.Begin);
                    responseStream.CopyTo(fileStream);
                    fileStream.Close();
                }
        }
コード例 #5
-3
ファイル: S3TestUtils.cs プロジェクト: aws/aws-sdk-net
        public static GetObjectResponse GetObjectHelper(AmazonS3Client client, string bucketName, string key)
        {
            GetObjectResponse r = null;
            Exception responseException = null;
            AutoResetEvent ars = new AutoResetEvent(false);

            client.GetObjectAsync(new GetObjectRequest()
            {
                BucketName = bucketName,
                Key = key
            }, (response) =>
            {
                responseException = response.Exception;
                if (responseException == null)
                {
                    r = response.Response;
                }
                ars.Set();
            }, new AsyncOptions { ExecuteCallbackOnMainThread = false });
            ars.WaitOne();

            if (responseException != null)
            {
                throw responseException;
            }
            return r;
        }