private Dictionary <string, Object> GetConfigOptions() { Amazon.S3.Model.GetObjectResponse rsp; IAWSContext ctx = AppUtility.GetContext(); var name = AppUtility.GetContext().Bucket; var key = "config"; string content; using (var client = ctx.GetS3Client()) { var req = new Amazon.S3.Model.GetObjectRequest() .WithBucketName(name).WithKey(key); try { rsp = client.GetObject(req); } catch (Exception ex) { Debug.WriteLine(String.Format("Bucket {0} key {1}: {2}", name, key, ex.Message), GetType()); throw; } using (StreamReader r = new StreamReader(rsp.ResponseStream)) { content = r.ReadToEnd(); } } var dict = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(content); return(dict); }
public Attachment GetStream(FileInfo file) { var bucketInfo = CreateBucketInfo(file.URL); var awsCredentials = new BasicAWSCredentials(McmModuleConfiguration.Aws.AccessKey, McmModuleConfiguration.Aws.SecretKey); var s3Config = new AmazonS3Config { ServiceURL = bucketInfo.ServiceURL }; using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(awsCredentials, s3Config)) { try { var request = new Amazon.S3.Model.GetObjectRequest { BucketName = bucketInfo.Bucketname, Key = bucketInfo.Key, }; var response = client.GetObject(request); return new Attachment { FileName = file.OriginalFilename, ContentType = file.MimeType, Disposable = response, Stream = response.ResponseStream }; } catch (System.Exception e) { throw new UnhandledException(string.Format("bucket: {0}, key: {1}, service_url: {2}", bucketInfo.Bucketname, bucketInfo.Key, bucketInfo.ServiceURL), e); } } }
public System.IO.Stream GetReadStream() { var request = new Amazon.S3.Model.GetObjectRequest { BucketName = _bucketName, Key = _keyName }; var response = _amazonS3.GetObjectAsync(request).Result; return(response.ResponseStream); }
async Task <IMailMessage> GetObject(Amazon.S3.Model.S3Object item) { var request = new Amazon.S3.Model.GetObjectRequest { Key = item.Key, BucketName = item.BucketName }; var response = await S3Client.GetObjectAsync(request); var sesMessage = MimeMessage.Load(response.ResponseStream); return(Account.CreateMailMessage(sesMessage, Account.S3Bucket)); }
public async Task <string> GetTextFile(string filePath, CancellationToken cancellationToken) { var client = GetClient(); var request = new Amazon.S3.Model.GetObjectRequest(); request.Key = filePath; request.BucketName = _bucketName; var response = await client.GetObjectAsync(request, cancellationToken); return(await ReadAllText(response.ResponseStream)); }
/// <summary> /// Gets an S3 object as a GetObjectResponse for /// access to other response properties. /// </summary> /// <param name="bucket">The bucket that the object is in.</param> /// <param name="key">The path to the object in S3.</param> public static Amazon.S3.Model.GetObjectResponse GetObjectResponse(string bucket, string key) { Amazon.S3.Model.GetObjectResponse response = new Amazon.S3.Model.GetObjectResponse(); using (Amazon.S3.IAmazonS3 client = new Factory().S3Client()) { Amazon.S3.Model.GetObjectRequest request = new Amazon.S3.Model.GetObjectRequest() { BucketName = bucket, Key = key }; response = client.GetObject(request); } return response; }
private async Task <Stream> Download(string objectKey, Amazon.S3.IAmazonS3 s3Client) { var request = new Amazon.S3.Model.GetObjectRequest() { BucketName = _config.BucketName, Key = objectKey }; var response = await s3Client.GetObjectAsync(request); return(response.ResponseStream); }
public ErrorTypes ReadFile(string strPath, System.IO.Stream oStream, out int nReadWriteBytes) { ErrorTypes eResult = ErrorTypes.StorageRead; nReadWriteBytes = 0; try { string strFileKey = GetFilePath(strPath); using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion)) { Amazon.S3.Model.GetObjectRequest oRequest = new Amazon.S3.Model.GetObjectRequest() .WithBucketName(m_strBucketName).WithKey(strFileKey); using (Amazon.S3.Model.GetObjectResponse oResponse = oS3Client.GetObject(oRequest)) { using (Stream oResponseStream = oResponse.ResponseStream) { int nNeedReadBytes = (int)oResponse.ContentLength; int nMemoryStreamSize = Math.Min(c_nMaxReadBufferSize, nNeedReadBytes); MemoryStream oMemoryStreamOutput = new MemoryStream(nMemoryStreamSize); while (nNeedReadBytes > 0) { int nReadBytesPos = 0; int nReadBytesCount = nMemoryStreamSize; int nReadBytesFromStreamCount = 0; while (nReadBytesCount > 0 && (nReadBytesFromStreamCount = oResponseStream.Read(oMemoryStreamOutput.GetBuffer(), nReadBytesPos, nReadBytesCount)) > 0) { nReadBytesPos += nReadBytesFromStreamCount; nReadBytesCount -= nReadBytesFromStreamCount; } oStream.Write(oMemoryStreamOutput.GetBuffer(), 0, nReadBytesPos); nReadWriteBytes += nReadBytesPos; nNeedReadBytes -= nReadBytesPos; } } } } } catch { } return(eResult); }
public async Task <IBlobData> Fetch(string virtualPath) { var mapping = mappings.FirstOrDefault(m => virtualPath.StartsWith(m.Prefix, m.IgnorePrefixCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)); if (mapping.Prefix == null) { return(null); } var partialKey = virtualPath.Substring(mapping.Prefix.Length).TrimStart('/'); if (mapping.LowercaseBlobPath) { partialKey = partialKey.ToLowerInvariant(); } var key = string.IsNullOrEmpty(mapping.BlobPrefix) ? partialKey : mapping.BlobPrefix + "/" + partialKey; try { using var client = new AmazonS3Client(credentials, mapping.Config); var req = new Amazon.S3.Model.GetObjectRequest() { BucketName = mapping.Bucket, Key = key }; var s = await client.GetObjectAsync(req); return(new S3Blob(s)); } catch (AmazonS3Exception se) { if (se.StatusCode == System.Net.HttpStatusCode.NotFound || "NoSuchKey".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase)) { throw new BlobMissingException($"Amazon S3 blob \"{key}\" not found.\n({se.Message})", se); } if (se.StatusCode == System.Net.HttpStatusCode.Forbidden || "AccessDenied".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase)) { throw new BlobMissingException($"Amazon S3 blob \"{key}\" not accessible. The blob may not exist or you may not have permission to access it.\n({se.Message})", se); } throw; } }
public static void Execute( request m, out HttpStatusCode hsc, out string status, string awsAccessKey, string awsSecretKey, out byte[] ba ) { ba = null; hsc = HttpStatusCode.BadRequest; status = ""; try { using (var s3c = new AmazonS3Client(awsAccessKey, awsSecretKey, m.re)) { var request = new Amazon.S3.Model.GetObjectRequest { BucketName = m.bucketName, Key = m.key, }; var response = s3c.GetObjectAsync(request).Result; using (var rs = response.ResponseStream) { using (var ms = new MemoryStream()) { rs.CopyTo(ms); ba = ms.ToArray(); } } } hsc = HttpStatusCode.OK; return; } catch (Exception ex) { LogIt.E(ex); hsc = HttpStatusCode.InternalServerError; status = "unexecpected error"; return; } finally { LogIt.I(JsonConvert.SerializeObject( new { hsc, status, m, //ipAddress = GetPublicIpAddress.Execute(hc), //executedBy = GetExecutingUsername.Execute() }, Formatting.Indented)); } }
public void ReadFileBegin(string strPath, System.IO.Stream oStream, AsyncCallback cb, object oParam) { try { m_oReadFile = new TransportClass(cb, oParam); m_oReadFile.m_oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion); string strFileKey = GetFilePath(strPath); Amazon.S3.Model.GetObjectRequest oRequest = new Amazon.S3.Model.GetObjectRequest() .WithBucketName(m_strBucketName).WithKey(strFileKey); m_oReadFile.m_oStreamOutput = oStream; m_oReadFile.m_oS3Client.BeginGetObject(oRequest, EndCallbackGetObject, m_oReadFile); } catch { m_oReadFile.m_eError = ErrorTypes.StorageRead; m_oReadFile.DisposeAndCallback(); } }
public async Task <ActionResult <string> > Get() { var awsAccessKey = Environment.GetEnvironmentVariable(ConfigKeys.g_env_ps_builds_aws_access_key); var awsSecretKey = Environment.GetEnvironmentVariable(ConfigKeys.g_env_ps_builds_aws_access_secret); var creds = new Amazon.Runtime.BasicAWSCredentials(awsAccessKey, awsSecretKey); var client = new AmazonS3Client(creds, Amazon.RegionEndpoint.USWest2); var getRequest = new Amazon.S3.Model.GetObjectRequest { BucketName = "playcompatibility", Key = "compat_summary.json" }; string responseBody; using (var response = await client.GetObjectAsync(getRequest)) { using (var reader = new StreamReader(response.ResponseStream)) { responseBody = reader.ReadToEnd(); } } return(Ok(responseBody)); }
public ErrorTypes ReadFile(string strPath, System.IO.Stream oStream, out int nReadWriteBytes) { ErrorTypes eResult = ErrorTypes.StorageRead; nReadWriteBytes = 0; try { string strFileKey = GetFilePath(strPath); using (Amazon.S3.AmazonS3 oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion)) { Amazon.S3.Model.GetObjectRequest oRequest = new Amazon.S3.Model.GetObjectRequest() .WithBucketName(m_strBucketName).WithKey(strFileKey); using (Amazon.S3.Model.GetObjectResponse oResponse = oS3Client.GetObject(oRequest)) { using (Stream oResponseStream = oResponse.ResponseStream) { int nNeedReadBytes = (int)oResponse.ContentLength; int nMemoryStreamSize = Math.Min(c_nMaxReadBufferSize, nNeedReadBytes); MemoryStream oMemoryStreamOutput = new MemoryStream(nMemoryStreamSize); while (nNeedReadBytes > 0) { int nReadBytesPos = 0; int nReadBytesCount = nMemoryStreamSize; int nReadBytesFromStreamCount = 0; while ( nReadBytesCount > 0 && ( nReadBytesFromStreamCount = oResponseStream.Read(oMemoryStreamOutput.GetBuffer(), nReadBytesPos, nReadBytesCount)) > 0 ) { nReadBytesPos += nReadBytesFromStreamCount; nReadBytesCount -= nReadBytesFromStreamCount; } oStream.Write(oMemoryStreamOutput.GetBuffer(), 0, nReadBytesPos); nReadWriteBytes += nReadBytesPos; nNeedReadBytes -= nReadBytesPos; } } } } } catch { } return eResult; }
// // GET: /S3Object/Edit/5 public ActionResult Edit(string id = null, string prefix = "", int maxKeys = 100) { ViewBag.prefix = prefix; ViewBag.maxKeys = maxKeys; var list = new Amazon.S3.Model.ListObjectsRequest(); list.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]); list.WithPrefix(id); var s3Objects = s3.ListObjects(list).S3Objects; if (s3Objects.Count == 0) { return HttpNotFound(); } var get = new Amazon.S3.Model.GetObjectRequest(); get.WithBucketName(WebConfigurationManager.AppSettings["UploadBucket"]); get.WithKey(s3Objects[0].Key); var response = s3.GetObject(get); S3Object modelObject = new S3Object { Key = s3Objects[0].Key, Size = s3Objects[0].Size, LastModified = s3Objects[0].LastModified, ContentType = response.ContentType }; return View(modelObject); }
public void ReadFileBegin(string strPath, System.IO.Stream oStream, AsyncCallback cb, object oParam) { try { m_oReadFile = new TransportClass(cb, oParam); m_oReadFile.m_oS3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(m_oRegion); string strFileKey = GetFilePath(strPath); Amazon.S3.Model.GetObjectRequest oRequest = new Amazon.S3.Model.GetObjectRequest() .WithBucketName(m_strBucketName).WithKey(strFileKey); m_oReadFile.m_oStreamOutput = oStream; m_oReadFile.m_oS3Client.BeginGetObject(oRequest, EndCallbackGetObject, m_oReadFile); } catch { m_oReadFile.m_eError = ErrorTypes.StorageRead; m_oReadFile.DisposeAndCallback(); } }
/// <summary> /// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used /// to respond to S3 notifications. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { //array for request ID's ArrayList requests = new ArrayList(); var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); //proccess input string responseBody = ""; string patientID = ""; Amazon.S3.Model.GetObjectRequest s3Request = new Amazon.S3.Model.GetObjectRequest { BucketName = s3Event.Bucket.Name, Key = s3Event.Object.Key }; using (Amazon.S3.Model.GetObjectResponse respond = await S3Client.GetObjectAsync(s3Request)) using (System.IO.Stream responseStream = respond.ResponseStream) using (System.IO.StreamReader reader = new System.IO.StreamReader(responseStream)) { try { responseBody = reader.ReadToEnd(); XmlDocument doc = new XmlDocument(); doc.LoadXml(responseBody); //parse XML //evaluates Xpath expressions XPathNavigator nav; //holds xml document XPathDocument docNav; //iterates through selected nodes XPathNodeIterator nodeIter; String idExpression = "/patient/id"; //opens xml docNav = new XPathDocument(new XmlNodeReader(doc)); //create a navigator to query with Xpath nav = docNav.CreateNavigator(); //gets id nodeIter = nav.Select(idExpression); nodeIter.MoveNext(); patientID = nodeIter.Current.Value; } catch (XmlException e) { //file is not xml Console.WriteLine("Error: {0}", e.Message); } } //send message to input queue String InputQueueURL = "https://sqs.us-east-1.amazonaws.com/799289016492/inputQueue"; AmazonSQSClient amazonSQSClient = new AmazonSQSClient(); //creating message //create requestID string requestID = Guid.NewGuid().ToString(); //Adds id to request id array requests.Add(requestID); string message = "{ \"RequestID\":\"" + requestID + "\", \"PatientID\":\"" + patientID + "\"}"; SendMessageRequest request = new SendMessageRequest { QueueUrl = InputQueueURL, MessageBody = message }; //sending message SendMessageResponse sendMessageResponse = amazonSQSClient.SendMessageAsync(request).Result; if (sendMessageResponse.HttpStatusCode.Equals(HttpStatusCode.OK)) { Console.WriteLine("Message successfully sent to queue {0}", InputQueueURL); } //wait for message from output String OutputQueueURL = " https://sqs.us-east-1.amazonaws.com/799289016492/outputQueue"; //while the request id array is not empty while (requests.Count > 0) { ReceiveMessageRequest outRequest = new ReceiveMessageRequest() { QueueUrl = OutputQueueURL, WaitTimeSeconds = 20 }; var outResponse = amazonSQSClient.ReceiveMessageAsync(outRequest); foreach (Message m in outResponse.Result.Messages) { //get message from output queue //converts string into JsonPatient object JsonPatient jp = JsonConvert.DeserializeObject <JsonPatient>(m.Body); //removes requestID from array requests.Remove(jp.RequestID); //handles response if (jp.Insurance) { Console.WriteLine("Patient with ID {0} has medical insurance", jp.PatientID); } else { Console.WriteLine("Patient with ID {0} does not have medical insurance", jp.PatientID); } //delete message from queue DeleteMessageRequest delete = new DeleteMessageRequest() { QueueUrl = OutputQueueURL, ReceiptHandle = m.ReceiptHandle }; amazonSQSClient.DeleteMessageAsync(delete); } } return(response.Headers.ContentType); } catch (Exception e) { context.Logger.LogLine($"Error getting object {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function."); context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
public async System.Threading.Tasks.Task <SoftmakeAll.SDK.OperationResult <System.Byte[]> > DownloadAsync(System.String BucketName, System.Collections.Generic.Dictionary <System.String, System.String> StorageFileNames) { SoftmakeAll.SDK.CloudStorage.AWS.Environment.Validate(); SoftmakeAll.SDK.OperationResult <System.Byte[]> OperationResult = new SoftmakeAll.SDK.OperationResult <System.Byte[]>(); if ((System.String.IsNullOrWhiteSpace(BucketName)) || (StorageFileNames == null) || (StorageFileNames.Count == 0)) { OperationResult.Message = "The BucketName and StorageFileName cannot be null."; return(OperationResult); } try { if (StorageFileNames.Count == 1) { Amazon.S3.Model.GetObjectRequest GetObjectRequest = new Amazon.S3.Model.GetObjectRequest { BucketName = BucketName, Key = StorageFileNames.First().Key }; using (Amazon.S3.Model.GetObjectResponse GetObjectResponse = await SoftmakeAll.SDK.CloudStorage.AWS.Environment._S3Client.GetObjectAsync(GetObjectRequest)) using (System.IO.Stream Stream = GetObjectResponse.ResponseStream) using (System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream()) { await Stream.CopyToAsync(MemoryStream); OperationResult.Data = MemoryStream.ToArray(); } } else { using (System.Threading.SemaphoreSlim SemaphoreSlim = new System.Threading.SemaphoreSlim(StorageFileNames.Count)) { System.Collections.Generic.List <System.Threading.Tasks.Task> DownloadTasks = new System.Collections.Generic.List <System.Threading.Tasks.Task>(); foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> StorageFileName in StorageFileNames) { await SemaphoreSlim.WaitAsync(); DownloadTasks.Add(System.Threading.Tasks.Task.Run(async() => { Amazon.S3.Model.GetObjectRequest GetObjectRequest = new Amazon.S3.Model.GetObjectRequest { BucketName = BucketName, Key = StorageFileName.Key }; using (Amazon.S3.Model.GetObjectResponse GetObjectResponse = await SoftmakeAll.SDK.CloudStorage.AWS.Environment._S3Client.GetObjectAsync(GetObjectRequest)) await GetObjectResponse.WriteResponseStreamToFileAsync(StorageFileName.Key, false, new System.Threading.CancellationToken()); SemaphoreSlim.Release(); })); } if (DownloadTasks.Any()) { await System.Threading.Tasks.Task.WhenAll(DownloadTasks); } } OperationResult.Data = SoftmakeAll.SDK.Files.Compression.CreateZipArchive(StorageFileNames); foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> StorageFileName in StorageFileNames) { if (System.IO.File.Exists(StorageFileName.Key)) { try { System.IO.File.Delete(StorageFileName.Key); } catch { } } } } } catch (System.Exception ex) { if (StorageFileNames.Count > 0) { foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> StorageFileName in StorageFileNames) { if (System.IO.File.Exists(StorageFileName.Key)) { try { System.IO.File.Delete(StorageFileName.Key); } catch { } } } } OperationResult.Message = ex.Message; return(OperationResult); } OperationResult.ExitCode = 0; return(OperationResult); }