Skip to content
Draft
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
21 changes: 5 additions & 16 deletions sdk/storage/storage-blob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ For a complete sample on iterating blobs please see [samples/v12/typescript/src/
```ts snippet:ReadmeSampleDownloadBlob_Node
import { BlobServiceClient } from "@azure/storage-blob";
import { DefaultAzureCredential } from "@azure/identity";
import { buffer } from "node:stream/consumers";

const account = "<account>";
const blobServiceClient = new BlobServiceClient(
Expand All @@ -399,22 +400,10 @@ const blobClient = containerClient.getBlobClient(blobName);
// In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody
const downloadBlockBlobResponse = await blobClient.download();
if (downloadBlockBlobResponse.readableStreamBody) {
const downloaded = await streamToString(downloadBlockBlobResponse.readableStreamBody);
console.log(`Downloaded blob content: ${downloaded}`);
}

async function streamToString(stream: NodeJS.ReadableStream): Promise<string> {
const result = await new Promise<Buffer<ArrayBuffer>>((resolve, reject) => {
const chunks: Buffer[] = [];
stream.on("data", (data) => {
chunks.push(Buffer.isBuffer(data) ? data : Buffer.from(data));
});
stream.on("end", () => {
resolve(Buffer.concat(chunks));
});
stream.on("error", reject);
});
return result.toString();
// Download the raw bytes of the blob. Use `text` from "node:stream/consumers"
// instead if you want to read the content as a string directly.
const downloaded = await buffer(downloadBlockBlobResponse.readableStreamBody);
console.log(`Downloaded blob content: ${downloaded.toString()}`);
}
```

Expand Down
13 changes: 7 additions & 6 deletions sdk/storage/storage-blob/samples-dev/errorsAndResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import { BlobServiceClient } from "@azure/storage-blob";

import { streamToBuffer } from "./utils/stream.js";
import { buffer } from "node:stream/consumers";
// Use `text` from "node:stream/consumers" if you want the content as a string directly.
// import { text } from "node:stream/consumers";

// Load the .env file if it exists
import "dotenv/config";
Expand Down Expand Up @@ -92,11 +94,10 @@ async function main(): Promise<void> {
console.log("// Download blob content...");
blockBlobClient = containerClient.getBlockBlobClient(blobName);
const downloadBlockBlobResponse = await blockBlobClient.download();
console.log(
`Downloaded blob content - ${(
await streamToBuffer(downloadBlockBlobResponse.readableStreamBody!)
).toString()},`,
);
// Download the raw bytes of the blob. Use `text(...)` from "node:stream/consumers"
// instead if you want to read the content as a string directly.
const downloaded = await buffer(downloadBlockBlobResponse.readableStreamBody!);
console.log(`Downloaded blob content - ${downloaded.toString()},`);
console.log(
`requestId - ${downloadBlockBlobResponse.requestId}, statusCode - ${downloadBlockBlobResponse._response.status}\n`,
);
Expand Down
12 changes: 7 additions & 5 deletions sdk/storage/storage-blob/samples-dev/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import { ContainerClient, StorageSharedKeyCredential } from "@azure/storage-blob";

import { streamToBuffer } from "./utils/stream.js";
import { buffer } from "node:stream/consumers";
// Use `text` from "node:stream/consumers" if you want the content as a string directly.
// import { text } from "node:stream/consumers";

// Load the .env file if it exists
import "dotenv/config";
Expand Down Expand Up @@ -65,10 +67,10 @@ async function main(): Promise<void> {
(await blobSnapshotClient.getProperties()).contentLength,
);

console.log(
"Downloaded blob content",
(await streamToBuffer(response.readableStreamBody!)).toString(),
);
// Download the raw bytes of the blob. Use `text(response.readableStreamBody!)`
// instead if you want to read the content as a string directly.
const downloaded = await buffer(response.readableStreamBody!);
console.log("Downloaded blob content", downloaded.toString());

// Delete container
await containerClient.delete();
Expand Down
16 changes: 0 additions & 16 deletions sdk/storage/storage-blob/samples-dev/utils/stream.ts

This file was deleted.

4 changes: 2 additions & 2 deletions sdk/storage/storage-blob/samples/v12/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ npm install
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):
Alternatively, run a single sample with the required 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
npx cross-env ACCOUNT_NAME="<account name>" ACCOUNT_KEY="<account key>" node sharedKeyAuth.js
```

## Next Steps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
* @summary use advanced HTTP pipeline and request options for several methods
*/

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

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

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

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");`
Expand All @@ -28,17 +26,17 @@ async function main() {

const pipeline = newPipeline(new AnonymousCredential(), {
// httpClient: MyHTTPClient, // A customized HTTP client implementing IHttpClient interface
retryOptions: { maxTries: 4 },
userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" },
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
`https://${account}.blob.core.windows.net${accountSas}`,
pipeline,
);

// Create a container
Expand All @@ -48,7 +46,7 @@ async function main() {
await containerClient.create();
} catch (err) {
console.log(
`Creating a container failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
`Creating a container failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
}

Expand All @@ -60,28 +58,28 @@ async function main() {
// BlockBlobClient.uploadFile() is only available in Node.js
try {
await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024,
concurrency: 20,
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}`
`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),
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}`
`uploadStream failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
}

Expand All @@ -102,15 +100,15 @@ async function main() {
const buffer = Buffer.alloc(fileSize);
try {
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortSignal.timeout(30 * 60 * 1000),
blockSize: 4 * 1024 * 1024,
concurrency: 20,
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}`
`downloadToBuffer failed, requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
}

Expand All @@ -123,7 +121,7 @@ async function main() {
} 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}`
`requestId - ${err.request.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`,
);
console.log(`error message - ${err.details.message}\n`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const { BlobServiceClient, AnonymousCredential } = require("@azure/storage-blob");

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

async function main() {
// Enter your storage account name and SAS
Expand All @@ -18,8 +18,8 @@ async function main() {
// 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()
`https://${account}.blob.core.windows.net${accountSas}`,
new AnonymousCredential(),
);

console.log("Containers:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { BlobServiceClient } = require("@azure/storage-blob");
const { DefaultAzureCredential } = require("@azure/identity");

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

async function main() {
// Enter your storage account name
Expand All @@ -50,7 +50,7 @@ async function main() {

const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
new DefaultAzureCredential()
new DefaultAzureCredential(),
);

// Create a container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const { BlobServiceClient } = require("@azure/storage-blob");

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

async function main() {
// Create Blob Service Client from Account connection string or SAS connection string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
} = require("@azure/storage-blob");

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

async function main() {
// Enter your storage account name and shared key
Expand All @@ -26,14 +26,14 @@ async function main() {
// 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 },
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
pipeline,
);

let i = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const {
} = require("@azure/storage-blob");

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

require("dotenv/config");
// Create a policy factory with create() method provided
class RequestIDPolicyFactory {
prefix;
Expand Down Expand Up @@ -50,7 +49,7 @@ class RequestIDPolicy extends BaseRequestPolicy {
// Customize client request ID header
request.headers.set(
"x-ms-client-request-id",
`${this.prefix}_SOME_PATTERN_${new Date().getTime()}`
`${this.prefix}_SOME_PATTERN_${new Date().getTime()}`,
);

// response is HttpOperationResponse type
Expand All @@ -71,11 +70,11 @@ async function main() {
const pipeline = newPipeline(new AnonymousCredential());

// Inject customized factory into default pipeline
pipeline.factories.unshift(new RequestIDPolicyFactory("Prefix"));
await pipeline.factories.unshift(new RequestIDPolicyFactory("Prefix"));

const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net?${accountSas}`,
pipeline
`https://${account}.blob.core.windows.net${accountSas}`,
pipeline,
);

const result = await blobServiceClient.listContainers().byPage().next();
Expand Down
Loading
Loading