-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from pauljewellmsft/pauljewell-lease-js
Add lease samples for JavaScript
- Loading branch information
Showing
4 changed files
with
869 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Azure Storage dependency | ||
const { BlobServiceClient } = require('@azure/storage-blob'); | ||
|
||
// Azure authentication for credential dependency | ||
const { DefaultAzureCredential } = require('@azure/identity'); | ||
|
||
// For development environment - include environment variables from .env | ||
require("dotenv").config(); | ||
|
||
// TODO: Replace with your actual storage account name | ||
const accountName = "<storage-account-name>"; | ||
|
||
async function main() { | ||
// create service client from DefaultAzureCredential | ||
const blobServiceClient = new BlobServiceClient( | ||
`https://${accountName}.blob.core.windows.net`, | ||
new DefaultAzureCredential() | ||
); | ||
|
||
const blobClient = blobServiceClient | ||
.getContainerClient("sample-container") | ||
.getBlobClient("sample-blob.txt"); | ||
|
||
// acquire lease | ||
const leaseClient = await acquireBlobLeaseAsync(blobClient); | ||
// output the blob lease status for testing | ||
console.log((await blobClient.getProperties()).leaseState); | ||
|
||
// renew lease | ||
await renewBlobLeaseAsync(blobClient, leaseClient.leaseId); | ||
|
||
// release lease | ||
await releaseBlobLeaseAsync(blobClient, leaseClient.leaseId); | ||
// output the blob lease status for testing | ||
console.log((await blobClient.getProperties()).leaseState); | ||
|
||
// break lease | ||
//await breakBlobLeaseAsync(blobClient); | ||
} | ||
|
||
// <Snippet_AcquireBlobLease> | ||
async function acquireBlobLeaseAsync(blobClient) { | ||
const leaseClient = blobClient.getBlobLeaseClient(); | ||
await leaseClient.acquireLease(30); | ||
return leaseClient; | ||
} | ||
// </Snippet_AcquireBlobLease> | ||
|
||
// <Snippet_RenewBlobLease> | ||
async function renewBlobLeaseAsync(blobClient, leaseID) { | ||
const leaseClient = blobClient.getBlobLeaseClient(leaseID); | ||
await leaseClient.renewLease(); | ||
} | ||
// </Snippet_RenewBlobLease> | ||
|
||
// <Snippet_ReleaseBlobLease> | ||
async function releaseBlobLeaseAsync(blobClient, leaseID) { | ||
const leaseClient = blobClient.getBlobLeaseClient(leaseID); | ||
await leaseClient.releaseLease(); | ||
} | ||
// </Snippet_ReleaseBlobLease> | ||
|
||
// <Snippet_BreakBlobLease> | ||
async function breakBlobLeaseAsync(blobClient) { | ||
const leaseClient = blobClient.getBlobLeaseClient(); | ||
await leaseClient.breakLease(); | ||
} | ||
// </Snippet_BreakBlobLease> | ||
|
||
main() | ||
.then(() => console.log(`done`)) | ||
.catch((ex) => console.log(ex.message)); |
70 changes: 70 additions & 0 deletions
70
blobs/howto/JavaScript/NodeJS-v12/dev-guide/lease-container.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Azure Storage dependency | ||
const { BlobServiceClient } = require('@azure/storage-blob'); | ||
|
||
// Azure authentication for credential dependency | ||
const { DefaultAzureCredential } = require('@azure/identity'); | ||
|
||
// For development environment - include environment variables from .env | ||
require("dotenv").config(); | ||
|
||
// TODO: Replace with your actual storage account name | ||
const accountName = "<storage-account-name>"; | ||
|
||
async function main() { | ||
// create service client from DefaultAzureCredential | ||
const blobServiceClient = new BlobServiceClient( | ||
`https://${accountName}.blob.core.windows.net`, | ||
new DefaultAzureCredential() | ||
); | ||
|
||
const blobContainerClient = blobServiceClient.getContainerClient("sample-container"); | ||
|
||
// acquire lease | ||
const leaseClient = await acquireContainerLeaseAsync(blobContainerClient); | ||
// output the blob lease status for testing | ||
console.log((await blobContainerClient.getProperties()).leaseState); | ||
|
||
// renew lease | ||
await renewContainerLeaseAsync(blobContainerClient, leaseClient.leaseId); | ||
|
||
// release lease | ||
await releaseContainerLeaseAsync(blobContainerClient, leaseClient.leaseId); | ||
// output the blob lease status for testing | ||
console.log((await blobContainerClient.getProperties()).leaseState); | ||
|
||
// break lease | ||
//await breakContainerLeaseAsync(blobContainerClient); | ||
} | ||
|
||
// <Snippet_AcquireContainerLease> | ||
async function acquireContainerLeaseAsync(blobContainerClient) { | ||
const leaseClient = blobContainerClient.getBlobLeaseClient(); | ||
await leaseClient.acquireLease(30); | ||
return leaseClient; | ||
} | ||
// </Snippet_AcquireContainerLease> | ||
|
||
// <Snippet_RenewContainerLease> | ||
async function renewContainerLeaseAsync(blobContainerClient, leaseID) { | ||
const leaseClient = blobContainerClient.getBlobLeaseClient(leaseID); | ||
await leaseClient.renewLease(); | ||
} | ||
// </Snippet_RenewContainerLease> | ||
|
||
// <Snippet_ReleaseContainerLease> | ||
async function releaseContainerLeaseAsync(blobContainerClient, leaseID) { | ||
const leaseClient = blobContainerClient.getBlobLeaseClient(leaseID); | ||
await leaseClient.releaseLease(); | ||
} | ||
// </Snippet_ReleaseContainerLease> | ||
|
||
// <Snippet_BreakContainerLease> | ||
async function breakContainerLeaseAsync(blobContainerClient) { | ||
const leaseClient = blobContainerClient.getBlobLeaseClient(); | ||
await leaseClient.breakLease(); | ||
} | ||
// </Snippet_BreakContainerLease> | ||
|
||
main() | ||
.then(() => console.log(`done`)) | ||
.catch((ex) => console.log(ex.message)); |
Oops, something went wrong.