我正在将一个私有文件从localhost上传到Amazon S3。上传后,如何从前端下载该特定文件。(JavaScript)
答案 0 :(得分:2)
您可以尝试s3.getObject
一些可以帮助您的链接: https://aws.amazon.com/sdk-for-node-js/
实施例: https://github.com/aws/aws-sdk-js/blob/e6671c4340d54010fb7dcf2eaebd679f565bf51b/ts/s3.ts
答案 1 :(得分:0)
首先编写(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 "";
}
}
第二次编写 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);
}});
}
从 HTML/C#/JQuery 的任何地方调用 downloadPrivateFile 方法 -
享受快乐的编码和复杂问题的解决