/** * Asserts that the blob has the correct blob type specified in the blob attributes. * * @throws StorageException * If an incorrect blob type is used. */ protected final void assertCorrectBlobType() throws StorageException { if (this instanceof CloudBlockBlob && this.properties.getBlobType() != BlobType.BLOCK_BLOB) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format(SR.INVALID_BLOB_TYPE, BlobType.BLOCK_BLOB, this.properties.getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } else if (this instanceof CloudPageBlob && this.properties.getBlobType() != BlobType.PAGE_BLOB) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format(SR.INVALID_BLOB_TYPE, BlobType.PAGE_BLOB, this.properties.getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } else if (this instanceof CloudAppendBlob && this.properties.getBlobType() != BlobType.APPEND_BLOB) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format(SR.INVALID_BLOB_TYPE, BlobType.APPEND_BLOB, this.properties.getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } }
/** * Asserts that the blob has the correct blob type specified in the blob attributes. * * @throws StorageException * If an incorrect blob type is used. */ protected final void assertCorrectBlobType() throws StorageException { if (this instanceof CloudBlockBlob && this.properties.getBlobType() != BlobType.BLOCK_BLOB) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format(SR.INVALID_BLOB_TYPE, BlobType.BLOCK_BLOB, this.properties.getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } else if (this instanceof CloudPageBlob && this.properties.getBlobType() != BlobType.PAGE_BLOB) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format(SR.INVALID_BLOB_TYPE, BlobType.PAGE_BLOB, this.properties.getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } else if (this instanceof CloudAppendBlob && this.properties.getBlobType() != BlobType.APPEND_BLOB) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format(SR.INVALID_BLOB_TYPE, BlobType.APPEND_BLOB, this.properties.getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } }
@Override public Void preProcessResponse(CloudBlob blob, CloudBlobClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK) { this.setNonExceptionedRetryableFailure(true); return null; } // Set attributes final BlobAttributes retrievedAttributes = BlobResponse.getBlobAttributes(this.getConnection(), blob.getStorageUri(), blob.snapshotID); if (retrievedAttributes.getProperties().getBlobType() != blob.properties.getBlobType()) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format( SR.INVALID_BLOB_TYPE, blob.properties.getBlobType(), retrievedAttributes.getProperties() .getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } blob.properties = retrievedAttributes.getProperties(); blob.metadata = retrievedAttributes.getMetadata(); return null; } };
@Override public Void preProcessResponse(CloudBlob blob, CloudBlobClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK) { this.setNonExceptionedRetryableFailure(true); return null; } // Set attributes final BlobAttributes retrievedAttributes = BlobResponse.getBlobAttributes(this.getConnection(), blob.getStorageUri(), blob.snapshotID); if (retrievedAttributes.getProperties().getBlobType() != blob.properties.getBlobType()) { throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, String.format( SR.INVALID_BLOB_TYPE, blob.properties.getBlobType(), retrievedAttributes.getProperties() .getBlobType()), Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } blob.properties = retrievedAttributes.getProperties(); blob.metadata = retrievedAttributes.getMetadata(); return null; } };
options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient, false /* setStartTime */);
@Override public HttpURLConnection buildRequest(CloudBlobClient client, CloudBlob blob, OperationContext context) throws Exception { this.setSendStream(sourceStream); this.setLength(length); return BlobRequest.putBlob(blob.getTransformedAddress(opContext).getUri(this.getCurrentLocation()), options, opContext, accessCondition, blob.properties, blob.properties.getBlobType(), this.getLength()); }
@Override public HttpURLConnection buildRequest(CloudBlobClient client, CloudBlob blob, OperationContext context) throws Exception { this.setSendStream(sourceStream); this.setLength(length); return BlobRequest.putBlob(blob.getTransformedAddress(opContext).getUri(this.getCurrentLocation()), options, opContext, accessCondition, blob.properties, blob.properties.getBlobType(), this.getLength()); }
@DoesServiceRequest private final boolean exists(final boolean primaryOnly, final AccessCondition accessCondition, BlobRequestOptions options, OperationContext opContext) throws StorageException { if (opContext == null) { opContext = new OperationContext(); } opContext.initialize(); options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient); return ExecutionEngine.executeWithRetry(this.blobServiceClient, this, this.existsImpl(primaryOnly, accessCondition, options), options.getRetryPolicyFactory(), opContext); }
@DoesServiceRequest private final boolean exists(final boolean primaryOnly, final AccessCondition accessCondition, BlobRequestOptions options, OperationContext opContext) throws StorageException { if (opContext == null) { opContext = new OperationContext(); } opContext.initialize(); options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient); return ExecutionEngine.executeWithRetry(this.blobServiceClient, this, this.existsImpl(primaryOnly, accessCondition, options), options.getRetryPolicyFactory(), opContext); }
options.requireEncryption(), null, blob.getProperties().getBlobType() == BlobType.PAGE_BLOB); return decryptStream; boolean noPadding = blob.getProperties().getBlobType() == BlobType.PAGE_BLOB || endOffset != null && endOffset < blobLength - 16; return new BlobDecryptStream(userProvidedStream, metadata, userSpecifiedLength,
options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient);
/** * Un-deletes a blob that has been soft-deleted, using the specified request options, and operation context. * <p> * The un-delete Blob operation restores the contents and metadata of soft deleted blob and all its snapshots. * Attempting to undelete a blob or snapshot that is not soft deleted will succeed without any changes. * * @param options * A {@link BlobRequestOptions} object that specifies any additional options for the request. Specifying * <code>null</code> will use the default request options from the associated service client ( * {@link CloudBlobClient}). * @param opContext * An {@link OperationContext} object that represents the context for the current operation. This object * is used to track requests to the storage service, and to provide additional runtime information about * the operation. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public final void undelete(BlobRequestOptions options, OperationContext opContext) throws StorageException { if (opContext == null) { opContext = new OperationContext(); } opContext.initialize(); options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient); ExecutionEngine.executeWithRetry(this.blobServiceClient, this, this.undeleteImpl(options), options.getRetryPolicyFactory(), opContext); }
/** * Downloads the contents of a blob to a stream using the specified request options and operation context. * * @param outStream * An <code>OutputStream</code> object that represents the target stream. * @param accessCondition * An {@link AccessCondition} object that represents the access conditions for the blob. * @param options * A {@link BlobRequestOptions} object that specifies any additional options for the request. Specifying * <code>null</code> will use the default request options from the associated service client ( * {@link CloudBlobClient}). * @param opContext * An {@link OperationContext} object that represents the context for the current operation. This object * is used to track requests to the storage service, and to provide additional runtime information about * the operation. * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public final void download(final OutputStream outStream, final AccessCondition accessCondition, BlobRequestOptions options, OperationContext opContext) throws StorageException { if (opContext == null) { opContext = new OperationContext(); } opContext.initialize(); options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient); ExecutionEngine.executeWithRetry(this.blobServiceClient, this, this.downloadToStreamImpl( null /* blobOffset */, null /* length */, outStream, accessCondition, options, opContext), options .getRetryPolicyFactory(), opContext); }
options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient);
@Override public CloudBlob preProcessResponse(CloudBlobContainer container, CloudBlobClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK) { this.setNonExceptionedRetryableFailure(true); return null; } // Set attributes final BlobAttributes retrievedAttributes = BlobResponse.getBlobAttributes(this.getConnection(), blobUri, snapshotID); CloudBlob blob; switch (retrievedAttributes.getProperties().getBlobType()) { case APPEND_BLOB: blob = container.getAppendBlobReference(blobName, snapshotID); break; case BLOCK_BLOB: blob = container.getBlockBlobReference(blobName, snapshotID); break; case PAGE_BLOB: blob = container.getPageBlobReference(blobName, snapshotID); break; default: throw new StorageException(StorageErrorCodeStrings.INCORRECT_BLOB_TYPE, SR.INVALID_RESPONSE_RECEIVED, Constants.HeaderConstants.HTTP_UNUSED_306, null, null); } blob.properties = retrievedAttributes.getProperties(); blob.metadata = retrievedAttributes.getMetadata(); return blob; } };
@Override public Void preProcessResponse(CloudBlob blob, CloudBlobClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK && this.getResult().getStatusCode() != HttpURLConnection.HTTP_ACCEPTED) { this.setNonExceptionedRetryableFailure(true); return null; } blob.updateEtagAndLastModifiedFromResponse(this.getConnection()); this.getResult().setRequestServiceEncrypted(BaseResponse.isServerRequestEncrypted(this.getConnection())); blob.getProperties().setBlobTierInferred(false); if (blob.getProperties().getBlobType() == BlobType.BLOCK_BLOB) { // For standard accounts when rehydrating a blob from archive, the status code will be 202 instead of 200. StandardBlobTier standardBlobTier = StandardBlobTier.parse(blobTierString); blob.getProperties().setRehydrationStatus(null); if (this.getResult().getStatusCode() == HttpURLConnection.HTTP_OK) { blob.getProperties().setStandardBlobTier(standardBlobTier); } else if (standardBlobTier.equals(StandardBlobTier.COOL)) { blob.getProperties().setStandardBlobTier(StandardBlobTier.ARCHIVE); } else if (standardBlobTier.equals(StandardBlobTier.HOT)) { blob.getProperties().setStandardBlobTier(StandardBlobTier.ARCHIVE); } } return null; }
@Override public Void preProcessResponse(CloudBlob blob, CloudBlobClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK && this.getResult().getStatusCode() != HttpURLConnection.HTTP_ACCEPTED) { this.setNonExceptionedRetryableFailure(true); return null; } blob.updateEtagAndLastModifiedFromResponse(this.getConnection()); this.getResult().setRequestServiceEncrypted(BaseResponse.isServerRequestEncrypted(this.getConnection())); blob.getProperties().setBlobTierInferred(false); if (blob.getProperties().getBlobType() == BlobType.BLOCK_BLOB) { // For standard accounts when rehydrating a blob from archive, the status code will be 202 instead of 200. StandardBlobTier standardBlobTier = StandardBlobTier.parse(blobTierString); blob.getProperties().setRehydrationStatus(null); if (this.getResult().getStatusCode() == HttpURLConnection.HTTP_OK) { blob.getProperties().setStandardBlobTier(standardBlobTier); } else if (standardBlobTier.equals(StandardBlobTier.COOL)) { blob.getProperties().setStandardBlobTier(StandardBlobTier.ARCHIVE); } else if (standardBlobTier.equals(StandardBlobTier.HOT)) { blob.getProperties().setStandardBlobTier(StandardBlobTier.ARCHIVE); } } return null; }
@Test public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); CloudPageBlob pageBlob = this.container.getPageBlobReference(blobName); try { pageBlob.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified blob does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } pageBlob.openWriteNew(1024); pageBlob.openWriteExisting(); CloudPageBlob pageBlob2 = this.container.getPageBlobReference(blobName); pageBlob2.downloadAttributes(); assertEquals(1024, pageBlob2.getProperties().getLength()); assertEquals(BlobType.PAGE_BLOB, pageBlob2.getProperties().getBlobType()); }
public static void assertAreEqual(BlobProperties prop1, BlobProperties prop2) { if (prop1 == null) { assertNull(prop2); } else { assertNotNull(prop2); assertEquals(prop1.getBlobType(), prop2.getBlobType()); assertEquals(prop1.getCacheControl(), prop2.getCacheControl()); assertEquals(prop1.getContentDisposition(), prop2.getContentDisposition()); assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding()); assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage()); assertEquals(prop1.getContentMD5(), prop2.getContentMD5()); assertEquals(prop1.getContentType(), prop2.getContentType()); assertEquals(prop1.getEtag(), prop2.getEtag()); assertEquals(prop1.getLastModified(), prop2.getLastModified()); assertEquals(prop1.getLength(), prop2.getLength()); assertEquals(prop1.getPageBlobSequenceNumber(), prop2.getPageBlobSequenceNumber()); } }
@Test public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); CloudAppendBlob appendBlob = this.container .getAppendBlobReference(blobName); try { appendBlob.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified blob does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } appendBlob.openWriteNew(); appendBlob.openWriteExisting(); CloudAppendBlob appendBlob2 = this.container .getAppendBlobReference(blobName); appendBlob2.downloadAttributes(); assertEquals(0, appendBlob2.getProperties().getLength()); assertEquals(BlobType.APPEND_BLOB, appendBlob2.getProperties().getBlobType()); }