public override async Task <IBlobMetadata> FetchMetadataAsync(string virtualPath, NameValueCollection queryString) { var path = ParseAndFilterPath(virtualPath); //Looks like we have to execute a head request var request = new GetObjectMetadataRequest() { BucketName = path.Bucket, Key = path.Key }; try { GetObjectMetadataResponse response = await S3Client.GetObjectMetadataAsync(request); return(new BlobMetadata() { Exists = true, LastModifiedDateUtc = response.LastModified }); } catch (AmazonS3Exception s3e) { if (s3e.StatusCode == System.Net.HttpStatusCode.NotFound || s3e.StatusCode == System.Net.HttpStatusCode.Forbidden) { return(new BlobMetadata() { Exists = false }); } else { throw; } } }
/// <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 { var response = await S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); string message = $"{s3Event.Object.Key} - {s3Event.Object.Size} Bytes"; context.Logger.LogLine(message); PublishRequest request = new PublishRequest { Message = message, PhoneNumber = "+34642375554" }; var smsResponse = await SnsClient.PublishAsync(request); context.Logger.LogLine($"Response from SNS: {smsResponse.HttpStatusCode}"); 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 Task <string> GetLatestVersionId(string bucketName, string objectKey) { GetObjectMetadataRequest request = new GetObjectMetadataRequest { BucketName = bucketName, Key = objectKey }; var response = await S3Client.GetObjectMetadataAsync(request); return(response.VersionId); }
public async Task <bool> ImageExists(string bucketName, string objectKey) { try { GetObjectMetadataRequest request = new GetObjectMetadataRequest { BucketName = bucketName, Key = objectKey }; await S3Client.GetObjectMetadataAsync(request); return true; } catch (AmazonS3Exception) { return(false); } }
private async Task <bool> DoesFileExist(string url, CancellationToken cancellationToken) { try { var response = await S3Client.GetObjectMetadataAsync("chdg-url-images", url, cancellationToken); return(true); } catch (AmazonS3Exception ex) { if (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { return(false); } //status wasn't not found, so throw the exception throw; } }