Skip to content

Commit ffa3de6

Browse files
committed
replace with code snippet syntax
1 parent 18bfec5 commit ffa3de6

File tree

1 file changed

+8
-99
lines changed

1 file changed

+8
-99
lines changed

articles/storage/blobs/storage-blob-upload-javascript.md

Lines changed: 8 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -37,121 +37,30 @@ Use the following table to find the correct upload method based on the blob clie
3737

3838
The following example uploads a local file to blob storage with the [BlockBlobClient](/javascript/api/@azure/storage-blob/blockblobclient) object. The [options](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions) object allows you to pass in your own metadata and [tags](storage-manage-find-blobs.md#blob-index-tags-and-data-management), used for indexing, at upload time:
3939

40-
```javascript
41-
// uploadOptions: {
42-
// metadata: { reviewer: 'john', reviewDate: '2022-04-01' },
43-
// tags: {project: 'xyz', owner: 'accounts-payable'}
44-
// }
45-
async function createBlobFromLocalPath(containerClient, blobName, localFileWithPath, uploadOptions){
46-
47-
// create blob client from container client
48-
const blockBlobClient = await containerClient.getBlockBlobClient(blobName);
49-
50-
// upload file to blob storage
51-
await blockBlobClient.uploadFile(localFileWithPath, uploadOptions);
52-
console.log(`${blobName} succeeded`);
53-
}
54-
```
40+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/upload-blob-from-local-file-path.js" id="Snippet_UploadBlob" highlight="14":::
5541

5642
## <a name="upload-by-using-a-stream"></a>Upload with BlockBlobClient by using a Stream
5743

5844
The following example uploads a readable stream to blob storage with the [BlockBlobClient](/javascript/api/@azure/storage-blob/blockblobclient) object. Pass in the BlockBlobUploadStream [options](/javascript/api/@azure/storage-blob/blockblobuploadstreamoptions) to affect the upload:
5945

60-
```javascript
61-
// uploadOptions: {
62-
// metadata: { reviewer: 'john', reviewDate: '2022-04-01' },
63-
// tags: {project: 'xyz', owner: 'accounts-payable'},
64-
// }
65-
async function createBlobFromReadStream(containerClient, blobName, readableStream, uploadOptions) {
66-
67-
// Create blob client from container client
68-
const blockBlobClient = await containerClient.getBlockBlobClient(blobName);
69-
70-
// Size of every buffer allocated, also
71-
// the block size in the uploaded block blob.
72-
// Default value is 8MB
73-
const bufferSize = 4 * 1024 * 1024;
74-
75-
// Max concurrency indicates the max number of
76-
// buffers that can be allocated, positive correlation
77-
// with max uploading concurrency. Default value is 5
78-
const maxConcurrency = 20;
79-
80-
// use transform per chunk - only to see chunck
81-
const transformedReadableStream = readableStream.pipe(myTransform);
82-
83-
// Upload stream
84-
await blockBlobClient.uploadStream(transformedReadableStream, bufferSize, maxConcurrency, uploadOptions);
85-
86-
// do something with blob
87-
const getTagsResponse = await blockBlobClient.getTags();
88-
console.log(`tags for ${blobName} = ${JSON.stringify(getTagsResponse.tags)}`);
89-
}
90-
91-
// Transform stream
92-
// Reasons to transform:
93-
// 1. Sanitize the data - remove PII
94-
// 2. Compress or uncompress
95-
const myTransform = new Transform({
96-
transform(chunk, encoding, callback) {
97-
// see what is in the artificially
98-
// small chunk
99-
console.log(chunk);
100-
callback(null, chunk);
101-
},
102-
decodeStrings: false
103-
});
104-
105-
```
46+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/upload-blob-from-stream.js" id="Snippet_UploadBlob" highlight="27":::
10647

107-
## <a name="upload-by-using-a-binarydata-object"></a>Upload with BlockBlobClient by using a BinaryData object
48+
Transform the stream during the upload for data clean up.
10849

109-
The following example uploads a Node.js buffer to blob storage with the [BlockBlobClient](/javascript/api/@azure/storage-blob/blockblobclient) object. Pass in the BlockBlobParallelUpload [options](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions) to affect the upload:
50+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/upload-blob-from-stream.js" id="Snippet_Transform" :::
11051

111-
```javascript
112-
// uploadOptions: {
113-
// blockSize: destination block blob size in bytes,
114-
// concurrency: concurrency of parallel uploading - must be greater than or equal to 0,
115-
// maxSingleShotSize: blob size threshold in bytes to start concurrency uploading
116-
// metadata: { reviewer: 'john', reviewDate: '2022-04-01' },
117-
// tags: {project: 'xyz', owner: 'accounts-payable'}
118-
// }
119-
async function createBlobFromBuffer(containerClient, blobName, buffer, uploadOptions) {
52+
## <a name="upload-by-using-a-binarydata-object"></a>Upload with BlockBlobClient by using a BinaryData object
12053

121-
// Create blob client from container client
122-
const blockBlobClient = await containerClient.getBlockBlobClient(blobName);
54+
The following example uploads a Node.js buffer to blob storage with the [BlockBlobClient](/javascript/api/@azure/storage-blob/blockblobclient) object. Pass in the BlockBlobParallelUpload [options](/javascript/api/@azure/storage-blob/blockblobparalleluploadoptions) to affect the upload:
12355

124-
// Upload buffer
125-
await blockBlobClient.uploadData(buffer, uploadOptions);
56+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/upload-blob-from-buffer.js" id="Snippet_UploadBlob" highlight="17":::
12657

127-
// do something with blob
128-
const getTagsResponse = await blockBlobClient.getTags();
129-
console.log(`tags for ${blobName} = ${JSON.stringify(getTagsResponse.tags)}`);
130-
}
131-
```
13258

13359
## <a name="upload-a-string"></a>Upload a string with BlockBlobClient
13460

13561
The following example uploads a string to blob storage with the [BlockBlobClient](/javascript/api/@azure/storage-blob/blockblobclient) object. Pass in the BlockBlobUploadOptions [options](/javascript/api/@azure/storage-blob/blockblobuploadoptions) to affect the upload:
13662

137-
```javascript
138-
// uploadOptions: {
139-
// metadata: { reviewer: 'john', reviewDate: '2022-04-01' },
140-
// tags: {project: 'xyz', owner: 'accounts-payable'}
141-
// }
142-
async function createBlobFromString(containerClient, blobName, fileContentsAsString, uploadOptions){
143-
144-
// Create blob client from container client
145-
const blockBlobClient = await containerClient.getBlockBlobClient(blobName);
146-
147-
// Upload string
148-
await blockBlobClient.upload(fileContentsAsString, fileContentsAsString.length, uploadOptions);
149-
150-
// do something with blob
151-
const getTagsResponse = await blockBlobClient.getTags();
152-
console.log(`tags for ${blobName} = ${JSON.stringify(getTagsResponse.tags)}`);
153-
}
154-
```
63+
:::code language="javascript" source="~/azure_storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/upload-blob-from-string.js" id="Snippet_UploadBlob" highlight="14":::
15564

15665
## See also
15766

0 commit comments

Comments
 (0)