我使用javascript,node.js和aws sdk。有很多關於通過簽名URL直接上傳現有文件到S3的例子,但是現在我正嘗試上傳字符串並在S3中創建一個文件,而沒有任何本地保存的文件。有什麼建議嗎?直接上傳字符串從瀏覽器到s3沒有本地文件
2
A
回答
1
沒有試過amazon-web-services
,amazon-s3
或aws-sdk
,但如果你能上傳File
或FormData
對象,可一方或雙方在JavaScript的創建和上傳對象。
// create a `File` object
const file = new File(["abc"], "file.txt", {type:"text/plain"});
// create a `Blob` object
// will be converted to a `File` object when passed to `FormData`
const blob = new Blob(["abc"], {type:"text/plain"});
const fd = new FormData();
fd.append("file", blob, "file.txt");
1
你可以嘗試這樣的事:
var fs = require('fs');
exports.upload = function (req, res) {
var file = req.files.file;
fs.readFile(file.path, function (err, data) {
if (err) throw err; // Something went wrong!
var s3bucket = new AWS.S3({params: {Bucket: 'mybucketname'}});
s3bucket.createBucket(function() {
var params = {
Key: file.originalFilename, //file.name doesn't exist as a property
Body: data
};
s3bucket.upload(params, function (err, data) {
// Whether there is an error or not, delete the temp file
fs.unlink(file.path, function (err) {
if (err) {
console.error(err);
}
console.log('Temp File Delete');
});
console.log("PRINT FILE:", file);
if (err) {
console.log('ERROR MSG: ', err);
res.status(500).send(err);
} else {
console.log('Successfully uploaded data');
res.status(200).end();
}
});
});
});
};
2
請按照這裏的例子 http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
轉換您的字符串來緩衝和傳遞。它應該工作。
相關問題
- 1. 直接從本地文件夾上傳文件,使用瀏覽器
- 2. 直接從GAE上傳文件到S3
- 3. 直接從瀏覽器上傳文件到Azure Blob存儲?
- 4. HTLM5文件直接輸入到瀏覽器緩存(沒有上傳/服務器)
- 5. 瀏覽器直接上傳到S3,準系統的Node.js
- 6. 從瀏覽器到本地連接iPhone的文件傳輸
- 7. 在IE8中可以從瀏覽器上傳文件到S3嗎?
- 8. 將文件從瀏覽器上傳到S3
- 9. 如何使用javascript從瀏覽器直接將文本字符串上傳到Windows Azure Blob
- 10. jQuery文件直接上傳到S3
- 11. 直接從瀏覽器上傳圖像到亞馬遜s3 - 使用角度js
- 12. 上傳到S3沒有本地密鑰?
- 13. 基於瀏覽器上傳到Amazon S3?
- 14. 如何直接從瀏覽器上傳文件到Google Cloud in Rails
- 15. 直接上傳到Cloudinary從瀏覽器錯誤的請求
- 16. 如何將文件從S3傳輸到瀏覽器?
- 17. Hadoop從本地機器上傳文件到亞馬遜s3
- 18. 直接從django上傳到S3
- 19. 直接上傳到S3從NAS
- 20. 跨瀏覽器鏈接到本地系統上的文件
- 21. 瀏覽器沒有從緩存中讀取Amazon S3文件
- 22. 上傳文件時沒有「瀏覽」
- 23. AWS S3通過瀏覽器直接下載文件
- 24. 從瀏覽器中的Flex/Flash播放器上傳到S3
- 25. 使用socket.io從字符串發送文件到瀏覽器
- 26. 直接上傳到Amazon S3從ASP.NET應用程序文件
- 27. 從瀏覽器訪問本地文件?
- 28. android httpservice從瀏覽器上傳文件
- 29. Ionic 2從瀏覽器上傳文件
- 30. 從瀏覽器自動上傳文件
AWS sdk接受文件,而不是字符串。這是我們需要的。謝謝。 – Bob