使用預先簽署的 URL 上傳檔案 
使用預先簽署的 URL,用戶端可以直接將檔案上傳到與 S3 相容的雲端儲存伺服器 (S3),而無需向使用者公開 S3 憑證。
本指南說明如何使用 presignedPutObject
API 從 MinIO JavaScript 程式庫 產生預先簽署的 URL。這透過 JavaScript 範例來展示,其中 Express Node.js 伺服器公開一個端點以產生預先簽署的 URL,而用戶端 Web 應用程式使用該 URL 將檔案上傳到 MinIO 伺服器。
1. 建立伺服器
伺服器由一個 Express Node.js 伺服器組成,該伺服器公開一個名為 /presignedUrl
的端點。此端點使用 Minio.Client
物件產生一個短暫的預先簽署 URL,可用於將檔案上傳到 Minio 伺服器。
// In order to use the MinIO JavaScript API to generate the pre-signed URL, begin by instantiating
// a `Minio.Client` object and pass in the values for your server.
// The example below uses values for play.min.io:9000
const Minio = require('minio')
var client = new Minio.Client({
endPoint: 'play.min.io',
port: 9000,
useSSL: true,
accessKey: 'Q3AM3UQ867SPQQA43P2F',
secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'
})
// Instantiate an `express` server and expose an endpoint called `/presignedUrl` as a `GET` request that
// accepts a filename through a query parameter called `name`. For the implementation of this endpoint,
// invoke [`presignedPutObject`](https://minio.dev.org.tw/docs/minio/linux/developers/javascript/API.html#presignedPutObjectt)
// on the `Minio.Client` instance to generate a pre-signed URL, and return that URL in the response:
// express is a small HTTP server wrapper, but this works with any HTTP server
const server = require('express')()
server.get('/presignedUrl', (req, res) => {
client.presignedPutObject('uploads', req.query.name, (err, url) => {
if (err) throw err
res.end(url)
})
})
server.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
})
server.listen(8080)
2. 建立用戶端 Web 應用程式
用戶端 Web 應用程式的使用者介面包含一個選取器欄位,允許使用者選取要上傳的檔案,以及一個按鈕,用於叫用一個名為 upload
的 onclick
處理常式。
<input type="file" id="selector" multiple>
<button onclick="upload()">Upload</button>
<div id="status">No uploads</div>
<script type="text/javascript">
// `upload` iterates through all files selected and invokes a helper function called `retrieveNewURL`.
function upload() {
// Get selected files from the input element.
var files = document.querySelector("#selector").files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Retrieve a URL from our server.
retrieveNewURL(file, (file, url) => {
// Upload the file to the server.
uploadFile(file, url);
});
}
}
// `retrieveNewURL` accepts the name of the current file and invokes the `/presignedUrl` endpoint to
// generate a pre-signed URL for use in uploading that file:
function retrieveNewURL(file, cb) {
fetch(`/presignedUrl?name=${file.name}`).then((response) => {
response.text().then((url) => {
cb(file, url);
});
}).catch((e) => {
console.error(e);
});
}
// ``uploadFile` accepts the current filename and the pre-signed URL. It then uses `Fetch API`
// to upload this file to S3 at `play.min.io:9000` using the URL:
function uploadFile(file, url) {
if (document.querySelector('#status').innerText === 'No uploads') {
document.querySelector('#status').innerHTML = '';
}
fetch(url, {
method: 'PUT',
body: file
}).then(() => {
// If multiple files are uploaded, append upload status on the next line.
document.querySelector('#status').innerHTML += `<br>Uploaded ${file.name}.`;
}).catch((e) => {
console.error(e);
});
}
</script>
注意: 這使用 File API、QuerySelector API、fetch API 和 Promise API。