Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Create a new Pipeline
const pipeline = StorageURL.newPipeline(new AnonymousCredential(), {
retryOptions: { maxTries: 4 },
telemetry: { value: 'HighLevelSample V1.0.0' }, // Customized telemetry string
keepAliveOptions: {
enable: false
}
});
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);
// Parallel Uploading
// We use a to() method to wrap the uploadStreamToBlockBlob so that
// instead of a try catch we can have it return the err as a result of the operation,
// similar to the older callback format.
let err, uploadStatus;
[err, uploadStatus] = await to(uploadStreamToBlockBlob(
Aborter.timeout(30 * 60 * 1000), // 30mins
createReadStream(dummyFilePath),
blockBlobURL,
fileSize,
20,
{
progress: ev => console.log(ev)
}
const pipeline = StorageURL.newPipeline(new AnonymousCredential(), {
retryOptions: { maxTries: 4 },
telemetry: { value: 'HighLevelSample V1.0.0' }, // Customized telemetry string
keepAliveOptions: {
enable: false
}
});
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)
}
);
await promiseLimit(5).map(files, async function(file: string) {
const blobURL = BlobURL.fromContainerURL(containerURL, file);
const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
const blobContentType = lookup(file) || '';
const blobContentEncoding = charset(blobContentType) || '';
await uploadStreamToBlockBlob(
Aborter.timeout(30 * 60 * 60 * 1000),
fs.createReadStream(path.join(filesPath, file)),
blockBlobURL,
4 * 1024 * 1024,
20,
{
blobHTTPHeaders: {
blobContentType,
blobContentEncoding
}
.map(async ({ name }) => {
console.log(`Setting content type for blob "${ name }"`);
const blobURL = BlobURL.fromContainerURL(containerURL, name);
await blobURL.setHTTPHeaders(
Aborter.timeout(BLOB_OPERATION_TIMEOUT),
{
blobContentType: TARGET_CONTENT_TYPE
}
)
})
);
export const exists = ({containerURL}) => async (key) => {
const blobURL = BlobURL.fromContainerURL(containerURL, key);
const getPropsResponse = await blobURL.getProperties();
return getPropsResponse._response.StatusCode === 200;
}
export const createFile = ({containerUrl}) => async (key, content) => {
const blobURL = BlobURL.fromContainerURL(containerURL, key);
const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
await blockBlobURL.upload(
Aborter.none,
content,
content.length
);
};
public async read(opts: ReadBlobOptions): Promise {
const containerURL = ContainerURL.fromServiceURL(this.service, opts.container);
const blobURL = BlobURL.fromContainerURL(containerURL, opts.path);
const stream = await blobURL.download(Aborter.none, 0)
return stream.readableStreamBody
}
}
export const loadFile = ({containerUrl}) => async key => {
const blobURL = BlobURL.fromContainerURL(
containerUrl, key);
const downloadBlockBlobResponse =
await blobURL.download(Aborter.none, 0);
return downloadBlockBlobResponse
.readableStreamBody
.read(content.length)
.toString();
};
export const deleteFile = ({containerURL}) => async key => {
const blobURL = BlobURL.fromContainerURL(
containerURL, key);
await blobURL.delete(Aborter.none);
}