Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async static Task Test(CognitoAWSCredentials credentials)
- {
- SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(10);
- List<Task> taskList = new List<Task>();
- AmazonS3Config config = new AmazonS3Config();
- config.RegionEndpoint = Amazon.RegionEndpoint.USEast1;
- AmazonS3Client client = new AmazonS3Client(credentials, config);
- foreach (string key in _fileNames) //_fileNames is an array of 320 S3 keys
- {
- await concurrencySemaphore.WaitAsync();
- Task task = Task.Run(
- async () =>
- {
- byte[] trash;
- try
- {
- Trace.Message("S3: Downloading key '" + key + "'");
- GetObjectRequest request = new GetObjectRequest()
- {
- BucketName = "LucentWearDesigns",
- Key = key
- };
- using (GetObjectResponse response = await client.GetObjectAsync(request))
- {
- using (BinaryReader reader = new BinaryReader(response.ResponseStream))
- {
- // Read data
- int length = (int)response.ContentLength;
- trash = reader.ReadBytes(length);
- }
- }
- }
- catch (Exception x)
- {
- Trace.Message("S3: error downloading key: " + key);
- Trace.Message(x.Message);
- Trace.Message(x.StackTrace);
- throw;
- }
- finally
- {
- concurrencySemaphore.Release();
- }
- });
- taskList.Add(task);
- }
- //Wait until all are finished
- await Task.WhenAll(taskList);
- Trace.Message("Synch: Success");
- }
Add Comment
Please, Sign In to add comment