/// <summary> /// Uploads the files. /// </summary> /// <param name="file">The file.</param> /// <param name="AccessKey">The access key.</param> /// <param name="SecretKey">The secret key.</param> /// <param name="BucketName">Name of the bucket.</param> /// <param name="FileName">Name of the file.</param> /// <returns></returns> public string Upload(Stream file, string FileName, int AttachmentId) { string response = string.Empty; IAmazonS3 client; AwsClient awsClientInstance = AwsClient.GetInstance; try { using (client = awsClientInstance.GetClient()) { var request = new PutObjectRequest() { Key = "Attachment_" + AttachmentId + "/" + FileName, BucketName = AwsClient.awsBucketName, CannedACL = S3CannedACL.PublicReadWrite, InputStream = file, //ContentType = "application/zip" }; var result = client.PutObjectAsync(request).Result; response = result.HttpStatusCode.ToString(); }; } catch (AmazonS3Exception ex) { response = ex.InnerException.Message; } return(response); }
/// <summary> /// Gets the signed URL. /// </summary> /// <param name="FileName">Name of the file.</param> /// <param name="AttachmentId">The attachment identifier.</param> /// <returns></returns> public string GetSignedURL(string FileName, int AttachmentId) { IAmazonS3 Client; AwsClient awsClientInstance = AwsClient.GetInstance; string response = string.Empty; try { using (var client = awsClientInstance.GetClient()) { var Request = new GetPreSignedUrlRequest() { BucketName = AwsClient.awsBucketName, Key = "Attachment_" + AttachmentId + "/" + FileName, Expires = DateTime.Now.AddYears(2) }; response = client.GetPreSignedURL(Request); } } catch (AmazonS3Exception e) { response = "Error encountered ***. Message:'{0}' when getting URL" + e.Message; } return(response); }
/// <summary> /// Gets the attachment from s3. /// </summary> /// <param name="AttachmentId">The attachment identifier.</param> /// <param name="file">The file.</param> /// <returns></returns> public byte[] GetAttachmentFromS3(int AttachmentId, Files file) { byte[] _byte = null; IAmazonS3 client; AwsClient awsClientInstance = AwsClient.GetInstance; using (client = awsClientInstance.GetClient()) { using (var bytesMemotyStream = new MemoryStream()) { var request = new GetObjectRequest() { BucketName = AwsClient.awsBucketName, Key = "Attachment_" + AttachmentId + "/" + file.FileName, }; using (GetObjectResponse responsefile = client.GetObjectAsync(request).Result) { using (Stream s = responsefile.ResponseStream) { s.CopyTo(bytesMemotyStream); } return(_byte = bytesMemotyStream.ToArray()); } } } }
/// <summary> /// Getfiles the information. /// </summary> /// <param name="AttachmentId">The attachment identifier.</param> public void GetfileInfo(int AttachmentId) { AwsClient awsClientInstance = AwsClient.GetInstance; IAmazonS3 client; using (client = awsClientInstance.GetClient()) { var request = new ListObjectsRequest() { BucketName = AwsClient.awsBucketName, Prefix = "Attachment_" + AttachmentId, }; var response = client.ListObjectsAsync(request).Result; }; }