Guest User

Untitled

a guest
Jul 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. public async static Task Test(CognitoAWSCredentials credentials)
  2. {
  3. SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(10);
  4. List<Task> taskList = new List<Task>();
  5.  
  6. AmazonS3Config config = new AmazonS3Config();
  7. config.RegionEndpoint = Amazon.RegionEndpoint.USEast1;
  8.  
  9. AmazonS3Client client = new AmazonS3Client(credentials, config);
  10.  
  11. foreach (string key in _fileNames) //_fileNames is an array of 320 S3 keys
  12. {
  13. await concurrencySemaphore.WaitAsync();
  14.  
  15. Task task = Task.Run(
  16. async () =>
  17. {
  18. byte[] trash;
  19. try
  20. {
  21. Trace.Message("S3: Downloading key '" + key + "'");
  22.  
  23. GetObjectRequest request = new GetObjectRequest()
  24. {
  25. BucketName = "LucentWearDesigns",
  26. Key = key
  27. };
  28.  
  29. using (GetObjectResponse response = await client.GetObjectAsync(request))
  30. {
  31. using (BinaryReader reader = new BinaryReader(response.ResponseStream))
  32. {
  33. // Read data
  34. int length = (int)response.ContentLength;
  35.  
  36. trash = reader.ReadBytes(length);
  37. }
  38. }
  39. }
  40. catch (Exception x)
  41. {
  42. Trace.Message("S3: error downloading key: " + key);
  43. Trace.Message(x.Message);
  44. Trace.Message(x.StackTrace);
  45. throw;
  46. }
  47. finally
  48. {
  49. concurrencySemaphore.Release();
  50. }
  51.  
  52. });
  53.  
  54. taskList.Add(task);
  55. }
  56.  
  57. //Wait until all are finished
  58. await Task.WhenAll(taskList);
  59. Trace.Message("Synch: Success");
  60. }
Add Comment
Please, Sign In to add comment