Created
December 17, 2019 10:44
-
-
Save OJezu/6248584c52f9a2095576d24b3f4bed94 to your computer and use it in GitHub Desktop.
Azure/azure-sdk-for-js#6577 cannot copy zero-length files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {BlobServiceClient, generateBlobSASQueryParameters} = require("@azure/storage-blob"); | |
require("dotenv").config(); | |
const connectionString = process.env.AZURE_TEST_CONNECTION_STRING; | |
const containerName = process.env.AZURE_TEST_CONTAINER; | |
// MAGIC VALUE, empty buffer causes errors/timeouts, non-empty buffer is copied | |
const content = Buffer.from(''); | |
async function getSignedUrl(blobClient){ | |
const expiry = 3600; | |
const startsOn = new Date(); | |
const expiresOn = new Date(new Date().valueOf() + expiry * 1000); | |
const token = await generateBlobSASQueryParameters( | |
{ | |
containerName: blobClient.containerName, | |
blobName: blobClient.location, | |
permissions: "r", // Required | |
startsOn, // Required | |
expiresOn, // Optional | |
}, | |
blobClient.credential, | |
); | |
return `${blobClient.url}?${token.toString()}`; | |
} | |
(async () => { | |
try { | |
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString); | |
const containerClient = blobServiceClient.getContainerClient(containerName); | |
const sourceBlobClient = containerClient.getBlockBlobClient('source-file'); | |
const targetBlobClient = containerClient.getBlockBlobClient('target-file'); | |
await sourceBlobClient.upload(content, content.byteLength); | |
const url = await getSignedUrl(sourceBlobClient); | |
console.log(`source: ${url}`); | |
await targetBlobClient.syncCopyFromURL(url); | |
console.log(`target: ${await getSignedUrl(targetBlobClient)}`); | |
} catch (e) { | |
console.log(e); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment