Se precisar apenas de parte dos dados de um arquivo, use o download por intervalo para recuperar dados em um intervalo especificado.
Observações de uso
Ao usar ferramentas de empacotamento como Webpack e Browserify, instale o OSS SDK for Browser.js executando o comando npm install ali-oss.
Se quiser acessar um bucket do OSS a partir de um navegador, mas não houver regras CORS configuradas para ele, o navegador rejeita a solicitação. Portanto, configure regras CORS para o bucket se quiser acessá-lo por um navegador. Para mais informações, consulte Installation.
-
Na maioria dos casos, o OSS SDK for Browser.js é usado em navegadores. Para evitar a exposição do seu par de AccessKey, recomendamos usar credenciais de acesso temporárias obtidas no Security Token Service (STS) para acessar o OSS.
As credenciais de acesso temporárias incluem um par de AccessKey e um token de segurança. O par de AccessKey é formado por um AccessKey ID e um AccessKeySecret. Para mais informações sobre como obter credenciais de acesso temporárias, consulte Use STS for temporary access authorization.
Código de exemplo
O código de exemplo a seguir mostra como baixar dados por intervalo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id='download'>Download</button>
<!--Import the SDK file.-->
<script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
<script type="text/javascript">
const client = new OSS({
// Set region to the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
region: 'yourRegion',
authorizationV4: true,
// The temporary AccessKey ID and AccessKey secret obtained from Security Token Service (STS).
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// The security token (SecurityToken) obtained from STS.
stsToken: 'yourSecurityToken',
// Specify the bucket name. For example, examplebucket.
bucket: "examplebucket",
});
// Get the DOM element.
const download = document.getElementById('download')
// For a 1000-byte file, the valid byte range is 0 to 999.
// If the specified range is invalid, for example, the start or end position is a negative value or is greater than the file size, the entire file is downloaded.
const start = 0, end = 999
// Add a listener to the button.
download.addEventListener('click', () => {
client.get('example.txt',{
headers: {
Range: `bytes=${start}-${end}`,
},
}).then(r => {
// Convert the returned data into a downloadable object in BLOB format.
const newBlob = new Blob([r.content], { type: r.res.headers['content-type'] });
// Create an anchor element.
const link = document.createElement('a')
// Attach the href property to the element.
link.href = window.URL.createObjectURL(newBlob)
// Specify the name of the local file.
link.download = 'example.txt'
// Download the object.
link.click()
// Remove the attached URL.
window.URL.revokeObjectURL(link.href)
})
})
</script>
</body>
</html>
Referências
Para obter o código de exemplo completo de downloads por intervalo, consulte o exemplo no GitHub.
Para acessar a referência da API de downloads por intervalo, consulte GetObject.