简体   繁体   English

如何通过 API 从 S3 存储桶下载私有文件

[英]How to dowload a private file from S3 bucket through API

I am uploading a private file from localhost to Amazon S3.我正在将私有文件从本地主机上传到 Amazon S3。 After uploading, how to download that particular file from front end.(JavaScript)上传后,如何从前端下载该特定文件。(JavaScript)

You can try s3.getObject你可以试试 s3.getObject

some links that can help You: https://aws.amazon.com/sdk-for-node-js/一些可以帮助您的链接: https ://aws.amazon.com/sdk-for-node-js/

Examples: https://github.com/aws/aws-sdk-js/blob/e6671c4340d54010fb7dcf2eaebd679f565bf51b/ts/s3.ts示例: https ://github.com/aws/aws-sdk-js/blob/e6671c4340d54010fb7dcf2eaebd679f565bf51b/ts/s3.ts

  1. Firstly write(C#) code to download private file as string首先编写(C#)代码将私人文件下载为字符串

     public string DownloadPrivateFileS3(string fileKey) { string accessKey = "YOURVALUE"; string accessSecret = "YOURVALUE";; string bucket = "YOURVALUE";; using (s3Client = new AmazonS3Client(accessKey, accessSecret, "YOURVALUE")) { var folderPath = "AppData/Websites/Cases"; var fileTransferUtility = new TransferUtility(s3Client); Stream stream = fileTransferUtility.OpenStream(bucket, folderPath + "/" + fileKey); using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); var response = memoryStream.ToArray(); return Convert.ToBase64String(response); } return ""; } }
  2. Second Write JQuery Code to download string as Base64第二次编写 JQuery 代码以将字符串下载为 Base64

 function downloadPrivateFile() { $.ajax({url: 'DownloadPrivateFileS3?fileName=' + fileName, success: function(result){ var link = this.document.createElement('a'); link.download = fileName; link.href = "data:application/octet-stream;base64," + result; this.document.body.appendChild(link); link.click(); this.document.body.removeChild(link); }}); }

Call downloadPrivateFile method from anywhere of HTML/C#/JQuery -从 HTML/C#/JQuery 的任何位置调用 downloadPrivateFile 方法 -

Enjoy Happy Coding and Solutions of Complex Problems享受复杂问题的快乐编码和解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM