Created
December 24, 2020 09:18
-
-
Save anishi1222/4bc241db32d8f257e8328a11196be1eb to your computer and use it in GitHub Desktop.
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
// Pessimistic | |
void pessimistic(BlobClient blobClient) { | |
BlobLeaseClient blobLeaseClient = new BlobLeaseClientBuilder().blobClient(blobClient).buildClient(); | |
try { | |
// 1st | |
String blobContents1 = "First update. Overwrite blob if it exists."; | |
byte[] byteArray = blobContents1.getBytes(StandardCharsets.UTF_8); | |
InputStream targetStream = new ByteArrayInputStream(byteArray); | |
BlobParallelUploadOptions blobParallelUploadOptions = new BlobParallelUploadOptions(targetStream, byteArray.length); | |
Response<BlockBlobItem> blobItemResponse = blobClient.uploadWithResponse(blobParallelUploadOptions, null, Context.NONE); | |
System.out.println("Content: " + blobContents1); | |
System.out.println("Status: " + blobItemResponse.getStatusCode()); | |
System.out.println("ETag: " + blobItemResponse.getValue().getETag()); | |
// Leaseを取得 | |
String leaseId = blobLeaseClient.acquireLease(15); | |
System.out.println("LeaseId: " + leaseId); | |
// 2nd Lease IDを取得しているので成功するはず | |
String blobContents2 = "Second update. Lease ID provided on request."; | |
byteArray = blobContents2.getBytes(StandardCharsets.UTF_8); | |
targetStream = new ByteArrayInputStream(byteArray); | |
BlobRequestConditions blobRequestConditions = new BlobRequestConditions().setLeaseId(leaseId); | |
blobParallelUploadOptions = new BlobParallelUploadOptions(targetStream, byteArray.length).setRequestConditions(blobRequestConditions); | |
blobItemResponse = blobClient.uploadWithResponse(blobParallelUploadOptions, null, Context.NONE); | |
System.out.println("Content: " + blobContents2); | |
System.out.println("Status: " + blobItemResponse.getStatusCode()); | |
System.out.println("ETag: " + blobItemResponse.getValue().getETag()); | |
// 3rd Lease IDを取得していないので失敗する | |
String blobContents3 = "Third update. No lease ID provided."; | |
byteArray = blobContents3.getBytes(StandardCharsets.UTF_8); | |
targetStream = new ByteArrayInputStream(byteArray); | |
blobParallelUploadOptions = new BlobParallelUploadOptions(targetStream, byteArray.length); | |
blobItemResponse = blobClient.uploadWithResponse(blobParallelUploadOptions, null, Context.NONE); | |
System.out.println("Content: " + blobContents3); | |
System.out.println("Status: " + blobItemResponse.getStatusCode()); | |
System.out.println("ETag: " + blobItemResponse.getValue().getETag()); | |
} | |
catch(BlobStorageException e) { | |
if(e.getStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED) { | |
System.out.println("Precondition failure as expected. The lease ID was not provided."); | |
} | |
else { | |
e.printStackTrace(); | |
} | |
} | |
finally { | |
blobLeaseClient.releaseLease(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment