/** * Return and access condition for this lease, or else null if * there's no lease. */ private AccessCondition getLeaseCondition(SelfRenewingLease lease) { AccessCondition leaseCondition = null; if (lease != null) { leaseCondition = AccessCondition.generateLeaseCondition(lease.getLeaseID()); } return leaseCondition; }
@Override public void uploadMetadata(OperationContext opContext) throws StorageException { container.uploadMetadata(AccessCondition.generateEmptyCondition(), null, opContext); }
/** * Generates a new empty AccessCondition. * <p> * For more information, see <a href= 'http://go.microsoft.com/fwlink/?LinkID=224642'>Specifying Conditional Headers * for Blob Service Operations</a>. * * @return An <code>AccessCondition</code> object that has no conditions set. */ public static AccessCondition generateEmptyCondition() { return new AccessCondition(); }
/** * Returns an access condition such that an operation will be performed only if the resource exists on the service. * <p> * Setting this access condition modifies the request to include the HTTP <i>If-Match</i> conditional header. * <p> * For more information, see <a href= 'http://go.microsoft.com/fwlink/?LinkID=224642'>Specifying Conditional Headers * for Blob Service Operations</a>. * * @return An <code>AccessCondition</code> object that represents the if exists condition. */ public static AccessCondition generateIfExistsCondition() { AccessCondition retCondition = new AccessCondition(); retCondition.setIfMatch("*"); return retCondition; }
/** * Returns an access condition such that an operation will be performed only if the resource does not exist on the * service. * <p> * Setting this access condition modifies the request to include the HTTP <i>If-None-Match</i> conditional header. * <p> * For more information, see <a href= 'http://go.microsoft.com/fwlink/?LinkID=224642'>Specifying Conditional Headers * for Blob Service Operations</a>. * * @return An <code>AccessCondition</code> object that represents the if not exists condition. */ public static AccessCondition generateIfNotExistsCondition() { AccessCondition retCondition = new AccessCondition(); retCondition.setIfNoneMatch("*"); return retCondition; }
AccessCondition appendCondition = new AccessCondition(); if (accessCondition != null) { appendCondition.setLeaseID(accessCondition.getLeaseID()); appendCondition.setIfAppendPositionEqual(accessCondition.getIfAppendPositionEqual()); appendCondition.setIfMaxSizeLessThanOrEqual(accessCondition.getIfMaxSizeLessThanOrEqual());
@Override public void recoveryAction(OperationContext context) throws IOException { if (this.getETagLockCondition() == null && (!Utility.isNullOrEmpty(this.getLockedETag()))) { AccessCondition etagLockCondition = new AccessCondition(); etagLockCondition.setIfMatch(this.getLockedETag()); if (accessCondition != null) { etagLockCondition.setLeaseID(accessCondition.getLeaseID()); } this.setETagLockCondition(etagLockCondition); } if (this.getCurrentRequestByteCount() > 0) { this.setOffset(startingOffset + this.getCurrentRequestByteCount()); if (length != null) { this.setLength(length - this.getCurrentRequestByteCount()); } } } };
/** * Releases the lease on the container. * * @param leaseID * The lease ID. */ public void releaseLease(String leaseID, String blobKey) throws Exception { AccessCondition accessCondition = new AccessCondition(); accessCondition.setLeaseID(leaseID); getBlobReference(blobKey).releaseLease(accessCondition); }
/** * Free the lease and stop the keep-alive thread. * @throws StorageException Thrown when fail to free the lease. */ public void free() throws StorageException { AccessCondition accessCondition = AccessCondition.generateEmptyCondition(); accessCondition.setLeaseID(leaseID); try { blobWrapper.getBlob().releaseLease(accessCondition); } catch (StorageException e) { if ("BlobNotFound".equals(e.getErrorCode())) { // Don't do anything -- it's okay to free a lease // on a deleted file. The delete freed the lease // implicitly. } else { // This error is not anticipated, so re-throw it. LOG.warn("Unanticipated exception when trying to free lease " + leaseID + " on " + blobWrapper.getStorageUri()); throw(e); } } finally { // Even if releasing the lease fails (e.g. because the file was deleted), // make sure to record that we freed the lease, to terminate the // keep-alive thread. leaseFreed = true; LOG.debug("Freed lease " + leaseID + " on " + blobWrapper.getUri() + " managed by thread " + renewer.getName()); } }
previousLeaseId = accessCondition.getLeaseID(); if (!accessCondition.verifyConditional(this.parentBlobRef.getProperties().getEtag(), this.parentBlobRef .getProperties().getLastModified())) { throw new StorageException(StorageErrorCode.CONDITION_FAILED.toString(), this.accessCondition = AccessCondition.generateIfMatchCondition(this.parentBlobRef.getProperties().getEtag()); this.accessCondition.setLeaseID(previousLeaseId);
accessCondition = AccessCondition.generateLeaseCondition(accessCondition.getLeaseID());
AccessCondition accessCondition = new AccessCondition(); accessCondition.setIfMaxSizeLessThanOrEqual(1024*2+1L); sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, -1, accessCondition, null, null); accessCondition = new AccessCondition(); accessCondition.setIfAppendPositionEqual(1024*2L); sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, -1, accessCondition, null, null); accessCondition = new AccessCondition(); accessCondition.setIfMaxSizeLessThanOrEqual(1024 - 1L); try { sourceStream = new ByteArrayInputStream(buffer); accessCondition = new AccessCondition(); accessCondition.setIfAppendPositionEqual(1024 - 1L); try { sourceStream = new ByteArrayInputStream(buffer);
previousLeaseId = accessCondition.getLeaseID(); this.accessCondition = AccessCondition.generateIfMatchCondition(this.parentFileRef.getProperties().getEtag()); this.accessCondition.setLeaseID(previousLeaseId);
@Test public void testAppendBlobMaxSizeCondition() throws URISyntaxException, StorageException, IOException { final int blobLength = 16 * 1024; String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blobRef = this.container .getAppendBlobReference(blobName); final byte[] buff = BlobTestHelper.getRandomBuffer(blobLength); ByteArrayInputStream sourceStream = new ByteArrayInputStream(buff); // Max length position failure AccessCondition cond = new AccessCondition(); cond.setIfMaxSizeLessThanOrEqual(blobLength - 1L); try { blobRef.upload(sourceStream, blobLength, cond, null, null); fail("Expected IOException for exceeding the max size"); } catch (IOException ex) { assertEquals(SR.INVALID_BLOCK_SIZE, ex.getMessage()); } }
@Override public void startCopyFromBlob(CloudBlobWrapper sourceBlob, BlobRequestOptions options, OperationContext opContext, boolean overwriteDestination) throws StorageException, URISyntaxException { AccessCondition dstAccessCondition = overwriteDestination ? null : AccessCondition.generateIfNotExistsCondition(); getBlob().startCopy(sourceBlob.getBlob().getQualifiedUri(), null, dstAccessCondition, options, opContext); }
IOException { CloudPageBlob blob1 = this.container.getPageBlobReference("blob1"); AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition("\"*\""); final int length = 6 * 512; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); accessCondition = AccessCondition.generateIfNoneMatchCondition(blob1.getProperties().getEtag()); try { blob1.upload(srcStream, length, accessCondition, null, null); accessCondition = AccessCondition.generateIfMatchCondition(blob1.getProperties().getEtag()); blob1.upload(srcStream, length, accessCondition, null, null); CloudPageBlob blob2 = this.container.getPageBlobReference("blob2"); blob2.create(1024); accessCondition = AccessCondition.generateIfMatchCondition(blob1.getProperties().getEtag()); try { blob1.upload(srcStream, length, accessCondition, null, null); accessCondition = AccessCondition.generateIfNoneMatchCondition(blob2.getProperties().getEtag()); blob1.upload(srcStream, length, accessCondition, null, null);
accessCondition.setLeaseID(lease.getLeaseID());
blob.uploadMetadata(AccessCondition.generateIfMatchCondition(currentETag), null, null); blob.uploadMetadata(AccessCondition.generateIfNoneMatchCondition(newETag), null, null); fail("If none match on conditional test should throw"); blob.uploadMetadata(AccessCondition.generateIfMatchCondition(invalidETag), null, null); fail("Invalid ETag on conditional test should throw"); blob.uploadMetadata(AccessCondition.generateIfNoneMatchCondition(invalidETag), null, null); blob.uploadMetadata(AccessCondition.generateIfModifiedSinceCondition(currentModifiedTime), null, null); fail("IfModifiedSince conditional on current modified time should throw"); cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) - 5); Date pastTime = cal.getTime(); blob.uploadMetadata(AccessCondition.generateIfModifiedSinceCondition(pastTime), null, null); cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - 5); pastTime = cal.getTime(); blob.uploadMetadata(AccessCondition.generateIfModifiedSinceCondition(pastTime), null, null); cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 5); pastTime = cal.getTime(); blob.uploadMetadata(AccessCondition.generateIfModifiedSinceCondition(pastTime), null, null); pastTime = cal.getTime(); try { blob.uploadMetadata(AccessCondition.generateIfNotModifiedSinceCondition(pastTime), null, null); fail("IfNotModifiedSince conditional on past time should throw");
cal.setTime(new Date()); cal.add(Calendar.MINUTE, 15); AccessCondition condition = AccessCondition.generateIfModifiedSinceCondition(cal.getTime()); condition = AccessCondition.generateIfNotExistsCondition(); try { blob.createOrReplace(condition, null, null);
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testBlobMultiConditionHeaders() throws URISyntaxException, StorageException, IOException { final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName); final int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); OperationContext context = new OperationContext(); blockBlobRef.upload(srcStream, -1, null, null, context); AccessCondition condition = AccessCondition.generateIfMatchCondition(context.getLastResult().getEtag()); condition.setIfUnmodifiedSinceDate(context.getLastResult().getStartDate()); StorageEvent<SendingRequestEvent> event = new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject(); assertNotNull(connection.getRequestProperty("If-Unmodified-Since")); assertNotNull(connection.getRequestProperty("If-Match")); } }; context.getSendingRequestEventHandler().addListener(event); blockBlobRef.upload(srcStream, -1, condition, null, context); }