Skip to content

Commit

Permalink
[Storage] Error handling in samples (#5803)
Browse files Browse the repository at this point in the history
* a new sample to show some example error codes, error messages and status codes of responses

* update advanced.ts sample

* Remove errorcode logging for successful response

And update/add JavaScript version of the samples.
  • Loading branch information
HarshaNalluru authored and jeremymeng committed Dec 6, 2019
1 parent a53a13c commit bd7ba32
Show file tree
Hide file tree
Showing 4 changed files with 426 additions and 40 deletions.
80 changes: 59 additions & 21 deletions sdk/storage/storage-blob/samples/javascript/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ async function main() {
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);
await containerClient.create();
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
const blobName = "newblob" + new Date().getTime();
Expand All @@ -46,43 +52,75 @@ async function main() {

// Parallel uploading with BlockBlobClient.uploadFile() in Node.js runtime
// BlockBlobClient.uploadFile() is only available in Node.js
await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
parallelism: 20, // 20 concurrency
progress: (ev) => console.log(ev)
});
console.log("uploadFile success");
try {
await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile succeeds");
} catch (err) {
console.log(
`uploadFile failed, requestId - ${err.details.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
await blockBlobClient.uploadStream(fs.createReadStream(localFilePath), 4 * 1024 * 1024, 20, {
abortSignal: AbortController.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
progress: (ev) => console.log(ev)
});
console.log("uploadStream success");
try {
await blockBlobClient.uploadStream(fs.createReadStream(localFilePath), 4 * 1024 * 1024, 20, {
abortSignal: AbortController.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.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
}

// Parallel uploading a browser File/Blob/ArrayBuffer in browsers with BlockBlobClient.uploadBrowserData()
// Uncomment following code in browsers because BlockBlobClient.uploadBrowserData() is only available in browsers
/*
const browserFile = document.getElementById("fileinput").files[0];
await blockBlobClient.uploadBrowserData(browserFile, {
blockSize: 4 * 1024 * 1024, // 4MB block size
parallelism: 20, // 20 concurrency
progress: ev => console.log(ev)
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);
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortController.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
blockSize: 4 * 1024 * 1024, // 4MB block size
parallelism: 20, // 20 concurrency
progress: (ev) => console.log(ev)
});
console.log("downloadToBuffer success");
try {
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortController.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.details.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.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
console.log(`error message - ${err.details.message}\n`);
}

// Delete container
await containerClient.delete();
Expand Down
155 changes: 155 additions & 0 deletions sdk/storage/storage-blob/samples/javascript/errorsAndResponses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
Setup: Enter connection string of your storage account name in main()
*/

const { BlobServiceClient } = require("../.."); // Change to "@azure/storage-blob" in your package

async function main() {
// Create Blob Service Client from Account connection string or SAS connection string
// Account connection string example - `DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=accountKey;EndpointSuffix=core.windows.net`
// SAS connection string example - `BlobEndpoint=https://myaccount.blob.core.windows.net/;QueueEndpoint=https://myaccount.queue.core.windows.net/;FileEndpoint=https://myaccount.file.core.windows.net/;TableEndpoint=https://myaccount.table.core.windows.net/;SharedAccessSignature=sasString`
const STORAGE_CONNECTION_STRING = process.env.STORAGE_CONNECTION_STRING || "";
// Note - Account connection string can only be used in node.
const blobServiceClient = BlobServiceClient.fromConnectionString(STORAGE_CONNECTION_STRING);

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

let createContainerResponse = await containerClient.create();
console.log(`Created container ${containerName} successfully,`);
console.log(
`requestId - ${createContainerResponse.requestId}, statusCode - ${createContainerResponse._response.status}\n`
);

try {
// Creating an existing container fails...
console.log("// Creating an existing container fails...");
createContainerResponse = await containerClient.create();
} catch (err) {
console.log(
`requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}\n`
);
}

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

try {
// Invoke getProperties() on a non existing blob
console.log("// Invoke getProperties() on a non existing blob...");
await blockBlobClient.getProperties();
} catch (err) {
console.log(`getProperties() failed as expected,`);
console.log(
`requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}\n`
);

// Create a new block blob
console.log("// Create a new block blob...");
const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
console.log(`Uploaded block blob ${blobName} successfully,`);
console.log(
`requestId - ${uploadBlobResponse.requestId}, statusCode - ${uploadBlobResponse._response.status}\n`
);
}

// Invoke getProperties() on an existing blob
console.log("// Invoke getProperties() on an existing blob...");
blockBlobClient = containerClient.getBlockBlobClient(blobName);
const blobProperties = await blockBlobClient.getProperties();
console.log(
`getProperties() on blob - ${blobName}, blobType = ${blobProperties.blobType}, accesTier = ${blobProperties.accessTier} `
);
console.log(
`requestId - ${blobProperties.requestId}, statusCode - ${blobProperties._response.status}\n`
);

try {
// Downloading from a non existing blob
console.log("// Downloading from a non existing blob...");
blockBlobClient = containerClient.getBlockBlobClient("invalid" + blobName);
await blockBlobClient.download();
} catch (err) {
console.log(`download() failed as expected,`);
console.log(
`requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}\n`
);

// Download blob content
console.log("// Download blob content...");
blockBlobClient = containerClient.getBlockBlobClient(blobName);
const downloadBlockBlobResponse = await blockBlobClient.download();
console.log(
`Downloaded blob content - ${await streamToString(
downloadBlockBlobResponse.readableStreamBody
)},`
);
console.log(
`requestId - ${downloadBlockBlobResponse.requestId}, statusCode - ${downloadBlockBlobResponse._response.status}\n`
);
}

try {
// Archive the blob
blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.setAccessTier("Archive");
// 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.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
console.log(`error message - ${err.details.message}\n`);
}

// Delete container
try {
// Deleting a non-existing container
console.log("// Deleting a non-existing container...");
containerClient = blobServiceClient.getContainerClient("invalid" + containerName);
await containerClient.delete();
} catch (err) {
console.log(`Deleting a non-existing container fails as expected`);
console.log(
`requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
console.log(`error message - \n${err.details.message}\n`);

// Delete container
containerClient = blobServiceClient.getContainerClient(containerName);
const deleteContainerResponse = await containerClient.delete();
console.log("Deleted container successfully -");
console.log(
`requestId - ${deleteContainerResponse.requestId}, statusCode - ${deleteContainerResponse._response.status}\n`
);
}
}

// A helper method used to read a Node.js readable stream into string
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}

// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("\n\nSuccessfully executed sample.");
})
.catch((err) => {
console.log(err.message);
});
76 changes: 57 additions & 19 deletions sdk/storage/storage-blob/samples/typescript/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ async function main() {
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);
await containerClient.create();
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
const blobName = "newblob" + new Date().getTime();
Expand All @@ -46,20 +52,32 @@ async function main() {

// Parallel uploading with BlockBlobClient.uploadFile() in Node.js runtime
// BlockBlobClient.uploadFile() is only available in Node.js
await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile success");
try {
await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile succeeds");
} catch (err) {
console.log(
`uploadFile failed, requestId - ${err.details.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
await blockBlobClient.uploadStream(fs.createReadStream(localFilePath), 4 * 1024 * 1024, 20, {
abortSignal: AbortController.timeout(30 * 60 * 1000), // Abort uploading with timeout in 30mins
onProgress: (ev) => console.log(ev)
});
console.log("uploadStream success");
try {
await blockBlobClient.uploadStream(fs.createReadStream(localFilePath), 4 * 1024 * 1024, 20, {
abortSignal: AbortController.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.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
}

// Parallel uploading a browser File/Blob/ArrayBuffer in browsers with BlockBlobClient.uploadBrowserData()
// Uncomment following code in browsers because BlockBlobClient.uploadBrowserData() is only available in browsers
Expand All @@ -76,13 +94,33 @@ async function main() {
// downloadToBuffer is only available in Node.js
const fileSize = fs.statSync(localFilePath).size;
const buffer = Buffer.alloc(fileSize);
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortController.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 success");
try {
await blockBlobClient.downloadToBuffer(buffer, 0, undefined, {
abortSignal: AbortController.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.details.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.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
console.log(`error message - ${err.details.message}\n`);
}

// Delete container
await containerClient.delete();
Expand Down
Loading

0 comments on commit bd7ba32

Please sign in to comment.