Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const serviceURL = new ServiceURL(
`https://${blobInfo.hostName}/${blobInfo.sasToken}`,
pipeline
);
// initialize the blockBlobURL to a new blob
const containerURL = ContainerURL.fromServiceURL(serviceURL, blobInfo.containerName);
const blobURL = BlobURL.fromContainerURL(containerURL, blobInfo.blobName);
const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
// get file stats
let fileStats = await getFileStats(localFilePath);
// parallel uploading
let uploadStatus = await uploadStreamToBlockBlob(
Aborter.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
fs.createReadStream(localFilePath),
blockBlobURL,
4 * 1024 * 1024, // 4MB block size
20, // 20 concurrency
{
progress: ev => console.log(ev)
}
);
console.log('uploadStreamToBlockBlob success');
// END STORAGE CODE
// notify IoT Hub of upload to blob status (success/faillure)
await client.notifyBlobUploadStatus(uploadStatus);
return 0;
}
public async downloadBinary(containerName: string, blobName: string, targetPath: string) {
const blockBlobUrl = this.getBlockBlobURL(containerName, blobName);
const props = await blockBlobUrl.getProperties(Aborter.none);
const buffer = Buffer.alloc(props.contentLength);
await downloadBlobToBuffer(
Aborter.timeout(30 * 60 * 1000),
buffer,
blockBlobUrl,
0,
undefined,
{
blockSize: 4 * 1024 * 1024, // 4MB block size
parallelism: 20, // 20 concurrency
}
);
fs.writeFileSync(targetPath, buffer, "binary");
}
files.map(file => {
// You should verify the URL if it is from the blob account and container that this service owned.
const blockBlobURL = new BlockBlobURL(file, pipeline);
// After the bot receives the URL, it should validate its content and associate it with the user ID.
// If the content needs to be kept for longer than a day, we should copy it to another blob container because we set up a rule to clean the storage daily.
return blockBlobURL.getProperties(Aborter.timeout(5000));
})
);
async function execute() {
const containerName = "demo";
const blobName = "quickstart.txt";
const content = "Hello Node SDK";
const localFilePath = "../readme.md";
const credentials = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);
const pipeline = StorageURL.newPipeline(credentials);
const serviceURL = new ServiceURL(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline);
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);
const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName);
const aborter = Aborter.timeout(30 * ONE_MINUTE);
await containerURL.create(aborter);
console.log(`Container: "${containerName}" is created`);
console.log("Containers:");
await showContainerNames(aborter, serviceURL);
await blockBlobURL.upload(aborter, content, content.length);
console.log(`Blob "${blobName}" is uploaded`);
await uploadLocalFile(aborter, containerURL, localFilePath);
console.log(`Local file "${localFilePath}" is uploaded`);
await uploadStream(aborter, containerURL, localFilePath);
console.log(`Local file "${localFilePath}" is uploaded as a stream`);
async function main(accountName, accountKey, container, prefix) {
const containerURL = ContainerURL.fromServiceURL(
new ServiceURL(
`https://${ accountName }.blob.core.windows.net`,
StorageURL.newPipeline(new SharedKeyCredential(accountName, accountKey))
),
container
);
const { segment } = await containerURL.listBlobHierarchySegment(
Aborter.timeout(BLOB_OPERATION_TIMEOUT),
BLOB_DELIMITER,
null,
{ prefix }
);
console.log([
`Found ${ segment.blobItems.length } blob in container "${ container }" with prefix "${ prefix || '' }"`,
...segment.blobItems.map(({ name, properties: { contentLength } }) => ` ${ name } (${ contentLength } bytes)`),
''
].join('\n'));
await Promise.all(
segment.blobItems
.filter(({ name }) => extname(name) === TARGET_EXTNAME)
.map(async ({ name }) => {
console.log(`Setting content type for blob "${ name }"`);
export async function setStaticSiteToPublic(serviceURL: ServiceURL) {
await serviceURL.setProperties(Aborter.timeout(30 * 60 * 60 * 1000), {
staticWebsite: {
enabled: true,
indexDocument: 'index.html',
errorDocument404Path: 'index.html'
}
});
}
constructor({
container,
indexBlobName = 'index.json',
timeout = 30 * 60000,
concurrentConnections = 5
}: AzureBlobStorageOptions) {
this.container = container;
this.indexBlobUrl = BlockBlobURL.fromContainerURL(this.container, indexBlobName);
this.aborter = Aborter.timeout(timeout);
this.concurrentConnections = concurrentConnections;
}