我正在嘗試創建一個反應應用程式,用戶可以在其中將檔案上傳到 S3 存盤桶。
我有一個可以成功從用戶那里獲取檔案的反應組件。還有一個按鈕,它呼叫我可以將檔案發送到 S3 的函式。
我正在使用react-aws-s3,因為這將是我需要的唯一直接功能,所以我不想安裝整個aws-sdk包并膨脹我的應用程式。
現在我已經關注了一些不同的博客/說明如何做到這一點(
這將為您提供有關您遇到的實際問題的更多資訊
您的存盤桶策略僅允許 List 和 Get,但在本教程中,它是s3: * 因此您的用戶必須有權將檔案上傳到此存盤桶。
仔細檢查演示中的上傳邏輯,如下所示:
const handleFileInput = (e) => {
setSelectedFile(e.target.files[0]);
}
const uploadFile = async (file) => {
const ReactS3Client = new S3(config);
// the name of the file uploaded is used to upload it to S3
ReactS3Client
.uploadFile(file, file.name)
.then(data => console.log(data.location))
.catch(err => console.error(err))
}
return <div>
<div>React S3 File Upload</div>
<input type="file" onChange={handleFileInput}/>
<br></br>
<button onClick={() => uploadFile(selectedFile)}> Upload to S3</button>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/490249.html
