Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sdk/storage/storage-blob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Release History

## 12.30.0-beta.2 (Unreleased)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes

## 12.30.0-beta.1 (2025-11-24)

### Features Added
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/storage-blob/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@azure/storage-blob",
"sdk-type": "client",
"version": "12.30.0-beta.1",
"version": "12.30.0-beta.2",
"description": "Microsoft Azure Storage SDK for JavaScript - Blob",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
87 changes: 87 additions & 0 deletions sdk/storage/storage-blob/samples/v12-beta/javascript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
page_type: sample
languages:
- javascript
products:
- azure
- azure-storage
urlFragment: storage-blob-javascript-beta
---

# Azure Storage Blob client library samples for JavaScript (Beta)

These sample programs show how to use the JavaScript client libraries for Azure Storage Blob in some common scenarios.

| **File Name** | **Description** |
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| [sharedKeyAuth.js][sharedkeyauth] | authenticate using an account name and a static key |
| [azureAdAuth.js][azureadauth] | authenticate with the storage service using Azure Active Directory |
| [anonymousAuth.js][anonymousauth] | authenticate anonymously using a SAS-encoded URL |
| [connectionStringAuth.js][connectionstringauth] | authenticate with the storage service using a connection string |
| [listBlobsByHierarchy.js][listblobsbyhierarchy] | list blobs by hierarchy, using separators in the blob names, using options for paging, resuming paging, etc. |
| [listBlobsFlat.js][listblobsflat] | list blobs in a container, showing options for paging, resuming paging, etc. |
| [listContainers.js][listcontainers] | list containers in an account, showing options for paging, resuming paging, etc. |
| [snapshots.js][snapshots] | create and read from a blob snapshot |
| [errorsAndResponses.js][errorsandresponses] | explores various error scenarios and their response data |
| [proxyAuth.js][proxyauth] | configure proxy settings when connecting to the storage service |
| [customizedClientHeaders.js][customizedclientheaders] | customize request headers such as `X-Ms-Client-Request-Id` using an HTTP policy |
| [advancedRequestOptions.js][advancedrequestoptions] | use advanced HTTP pipeline and request options for several methods |
| [customPipeline.js][custompipeline] | use custom HTTP pipeline options when connecting to the service |

## Prerequisites

The sample programs are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule).

You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs:

- [Azure Storage Account][createinstance_azurestorageaccount]

Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.

Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package].

## Setup

To run the samples using the published version of the package:

1. Install the dependencies using `npm`:

```bash
npm install
```

2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically.

3. Run whichever samples you like (note that some samples may require additional setup, see the table above):

```bash
node sharedKeyAuth.js
```

Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):

```bash
cross-env ACCOUNT_NAME="<account name>" ACCOUNT_KEY="<account key>" node sharedKeyAuth.js
```

## Next Steps

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.

[sharedkeyauth]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/sharedKeyAuth.js
[azureadauth]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/azureAdAuth.js
[anonymousauth]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/anonymousAuth.js
[connectionstringauth]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/connectionStringAuth.js
[listblobsbyhierarchy]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/listBlobsByHierarchy.js
[listblobsflat]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/listBlobsFlat.js
[listcontainers]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/listContainers.js
[snapshots]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/snapshots.js
[errorsandresponses]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/errorsAndResponses.js
[proxyauth]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/proxyAuth.js
[customizedclientheaders]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/customizedClientHeaders.js
[advancedrequestoptions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/advancedRequestOptions.js
[custompipeline]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/storage/storage-blob/samples/v12-beta/javascript/customPipeline.js
[apiref]: https://learn.microsoft.com/javascript/api/@azure/storage-blob?view=azure-node-preview
[freesub]: https://azure.microsoft.com/free/
[createinstance_azurestorageaccount]: https://learn.microsoft.com/azure/storage/common/storage-account-overview
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary use advanced HTTP pipeline and request options for several methods
*/

const fs = require("node:fs");

const { AnonymousCredential, BlobServiceClient, newPipeline } = require("@azure/storage-blob");

// Load the .env file if it exists
require("dotenv/config");
// Enabling logging may help uncover useful information about failures.
// In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`.
// Alternatively, logging can be enabled at runtime by calling `setLogLevel("info");`
// `setLogLevel` can be imported from the `@azure/logger` package
const { setLogLevel } = require("@azure/logger");
setLogLevel("info");

async function main() {
// Fill in following settings before running this sample
const account = process.env.ACCOUNT_NAME || "<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 failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
}

// Create a blob
const blobName = "newblob" + new Date().getTime();
const blockBlobClient = containerClient.getBlockBlobClient(blobName);

// Parallel uploading with BlockBlobClient.uploadFile() in Node.js runtime
// BlockBlobClient.uploadFile() is only available in Node.js
try {
await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev),
});
console.log("Successfully uploaded file:", blockBlobClient.name);
} catch (err) {
console.log(
`uploadFile failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
}

// Parallel uploading a Readable stream with BlockBlobClient.uploadStream() in Node.js runtime
// BlockBlobClient.uploadStream() is only available in Node.js
try {
await blockBlobClient.uploadStream(fs.createReadStream(localFilePath), 4 * 1024 * 1024, 20, {
abortSignal: AbortSignal.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
onProgress: (ev) => console.log(ev),
});
console.log("uploadStream succeeds");
} catch (err) {
console.log(
`uploadStream failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
}

// Parallel uploading a browser File/Blob/ArrayBuffer in browsers with BlockBlobClient.uploadData()
// Uncomment following code in browsers because document is only available in browsers
/*
const browserFile = document.getElementById("fileinput").files[0];
await blockBlobClient.uploadData(browserFile, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: ev => console.log(ev)
});
*/

// Parallel downloading a block blob into Node.js buffer
// downloadToBuffer is only available in Node.js
const fileSize = fs.statSync(localFilePath).size;
const buffer = Buffer.alloc(fileSize);
try {
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortSignal.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev),
});
console.log("downloadToBuffer succeeds");
} catch (err) {
console.log(
`downloadToBuffer failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
}

// Archive the blob - Log the error codes
await blockBlobClient.setAccessTier("Archive");
try {
// Downloading an archived blockBlob fails
console.log("// Downloading an archived blockBlob fails...");
await blockBlobClient.download();
} catch (err) {
// BlobArchived Conflict (409) This operation is not permitted on an archived blob.
console.log(
`requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
console.log(`error message - ${err.details.message}\n`);
}

// Delete container
await containerClient.delete();
console.log("Deleted container:", containerClient.containerName);
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary authenticate anonymously using a SAS-encoded URL
*/

const { BlobServiceClient, AnonymousCredential } = require("@azure/storage-blob");

// Load the .env file if it exists
require("dotenv/config");

async function main() {
// Enter your storage account name and SAS
const account = process.env.ACCOUNT_NAME || "<account name>";
const accountSas = process.env.ACCOUNT_SAS || "<account SAS>";

// 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}`,
new AnonymousCredential(),
);

console.log("Containers:");
for await (const container of blobServiceClient.listContainers()) {
console.log(`- ${container.name}`);
}

// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);

const createContainerResponse = await containerClient.create();
console.log(`Created container ${containerName} successfully`, createContainerResponse.requestId);

// Delete container
await containerClient.delete();

console.log("Deleted container:", containerClient.containerName);
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
Loading