Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Fill in following settings before running this sample
const account = process.env.ACCOUNT_NAME || "";
const accountSas = process.env.ACCOUNT_SAS || "";
const localFilePath = "README.md";
const pipeline = newPipeline(new AnonymousCredential(), {
// httpClient: MyHTTPClient, // A customized HTTP client implementing IHttpClient interface
retryOptions: { maxTries: 4 }, // Retry options
userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" }, // Customized telemetry string
keepAliveOptions: {
// Keep alive is enabled by default, disable keep alive by setting false
enable: false
}
});
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net${accountSas}`,
pipeline
);
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);
try {
await containerClient.create();
} catch (err) {
console.log(
`Creating a container fails, requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
}
// Create a blob
export async function main() {
// Enter your storage account name and shared key
const account = process.env.ACCOUNT_NAME || "";
const accountKey = process.env.ACCOUNT_KEY || "";
// Use StorageSharedKeyCredential with storage account and account key
// StorageSharedKeyCredential is only avaiable in Node.js runtime, not in browsers
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
// List containers
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
let i = 1;
for await (const container of blobServiceClient.listContainers()) {
console.log(`Container ${i++}: ${container.name}`);
}
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);
const createContainerResponse = await containerClient.create();
console.log(`Create container ${containerName} successfully`, createContainerResponse.requestId);
export async function main() {
// Enter your storage account name and shared key
const account = process.env.ACCOUNT_NAME || "";
const accountKey = process.env.ACCOUNT_KEY || "";
// Use StorageSharedKeyCredential with storage account and account key
// StorageSharedKeyCredential is only avaiable in Node.js runtime, not in browsers
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
// To use the manual proxyOptions below, remove this block
if (!process.env.HTTP_PROXY || !process.env.HTTPS_PROXY) {
console.warn("Proxy information not provided, but it is required to run this sample. Exiting.");
return;
}
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential,
// The library tries to load the proxy settings from the environment variables like HTTP_PROXY
// Alternatively, the service client accepts the following `proxyOptions` as part of its options:
{
/*
proxyOptions : {
// To use these options, remove the section above that checks for HTTP_PROXY or HTTPS_PROXY
host: "http://localhost",
port: 3128,
username: "",
password: ""
}
*/
}
);
const content = "hello";
const blobName = "newblob" + new Date().getTime();
const containerName = `newcontainer${new Date().getTime()}`;
let blobInfo = await client.getBlobSharedAccessSignature(blobName);
if (!blobInfo) {
throw new errors.ArgumentError('Invalid upload parameters');
}
// STORAGE BLOB CODE
const sharedKeyCredential = new SharedKeyCredential(account, accountKey);
let i = 1;
for await (const container of blobServiceClient.listContainers()) {
console.log(`Container ${i++}: ${container.name}`);
}
const blobServiceClient = new BlobServiceClient(
// When using AnonymousCredential, following url should include a valid SAS or support public access
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
// Create a container
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
const blockBlobClient = blobClient.getBlockBlobClient();
const createContainerResponse = await containerClient.create();
console.log(`Create container ${containerName} successfully`, createContainerResponse.requestId);
// Create a blob
let uploadError;
}
// ONLY AVAILABLE IN NODE.JS RUNTIME
// DefaultAzureCredential will first look for Azure Active Directory (AAD)
// client secret credentials in the following environment variables:
//
// - AZURE_TENANT_ID: The ID of your AAD tenant
// - AZURE_CLIENT_ID: The ID of your AAD app registration (client)
// - AZURE_CLIENT_SECRET: The client secret for your AAD app registration
//
// If those environment variables aren't found and your application is deployed
// to an Azure VM or App Service instance, the managed service identity endpoint
// will be used as a fallback authentication source.
const defaultAzureCredential = new DefaultAzureCredential();
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
defaultAzureCredential
);
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const createContainerResponse = await blobServiceClient
.getContainerClient(containerName)
.create();
console.log(`Created container ${containerName} successfully`, createContainerResponse.requestId);
}
export async function main() {
// Enter your storage account name and SAS
const account = process.env.ACCOUNT_NAME || "";
const accountSas = process.env.ACCOUNT_SAS || "";
// Use AnonymousCredential when url already includes a SAS signature
const anonymousCredential = new AnonymousCredential();
// List containers
const blobServiceClient = new BlobServiceClient(
// When using AnonymousCredential, following url should include a valid SAS or support public access
`https://${account}.blob.core.windows.net${accountSas}`,
anonymousCredential
);
let i = 1;
for await (const container of blobServiceClient.listContainers()) {
console.log(`Container ${i++}: ${container.name}`);
}
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);
const createContainerResponse = await containerClient.create();
console.log(`Create container ${containerName} successfully`, createContainerResponse.requestId);
const account = process.env.ACCOUNT_NAME || "";
const accountKey = process.env.ACCOUNT_KEY || "";
// Use StorageSharedKeyCredential with storage account and account key
// StorageSharedKeyCredential is only avaiable in Node.js runtime, not in browsers
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
// Use sharedKeyCredential, tokenCredential or anonymousCredential to create a pipeline
const pipeline = newPipeline(sharedKeyCredential, {
// httpClient: MyHTTPClient, // A customized HTTP client implementing IHttpClient interface
retryOptions: { maxTries: 4 }, // Retry options
userAgentOptions: { userAgentPrefix: "Sample V1.0.0" } // Customized telemetry string
});
// List containers
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
pipeline
);
let i = 1;
for await (const container of blobServiceClient.listContainers()) {
console.log(`Container ${i++}: ${container.name}`);
}
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);
const createContainerResponse = await containerClient.create();
console.log(`Create container ${containerName} successfully`, createContainerResponse.requestId);
BlobSASPermissions,
ContainerClient,
BlobSASSignatureValues
} from "@azure/storage-blob";
import {
BlockBlobUploadResponse
} from "@azure/storage-blob/typings/src/generated/src/models";
import { STORAGE_ACCOUNT, STORAGE_ACCOUNT_KEY } from "../../util/secrets";
import { UploadBlobResult } from "../storage.d";
// Use SharedKeyCredential with storage account and account key
const sharedKeyCredential: StorageSharedKeyCredential = new StorageSharedKeyCredential(
STORAGE_ACCOUNT,
STORAGE_ACCOUNT_KEY);
const blobServiceClient: BlobServiceClient = new BlobServiceClient(
`https://${STORAGE_ACCOUNT}.blob.core.windows.net`,
sharedKeyCredential
);
export const uploadBlob = async (
stream: NodeJS.ReadableStream,
contentLength: number,
containerName: string,
blobName: string): Promise => {
// Create container if it is not existing
let existing: boolean = false;
for await (const container of blobServiceClient.listContainers()) {
if (containerName === container.name) {
existing = true;
break;
}
static async Run() {
console.log(BlobStorage.dedent`
------------------------
Storage - Blobs
------------------------
1) Upload Blob
2) Delete Blob (Clean up the resource)
`);
const account = process.env["STORAGE_ACCOUNT_NAME"] || "";
const accountKey = process.env["STORAGE_ACCOUNT_KEY"] || "";
const containerName = "mycontainer";
BlobStorage.blobName = `JSNewBlob-${uuidv1()}.txt`;
const credential = new SharedKeyCredential(account, accountKey);
const serviceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
credential
);
BlobStorage.ContainerClient = serviceClient.getContainerClient(containerName);
//Ensure that the blob does not already existis
try {
await BlobStorage.CleanUp();
} catch { }
await BlobStorage.UploadBlob();
await BlobStorage.CleanUp();
}