com.microsoft.azure.storage.StorageErrorCodeStrings Java Examples

The following examples show how to use com.microsoft.azure.storage.StorageErrorCodeStrings. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AzureStorageService.java    From crate with Apache License 2.0 7 votes vote down vote up
public void writeBlob(String container, String blobName, InputStream inputStream, long blobSize,
                      boolean failIfAlreadyExists)
    throws URISyntaxException, StorageException, IOException {
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {})", blobName, blobSize));
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
    try {
        final AccessCondition accessCondition =
            failIfAlreadyExists ? AccessCondition.generateIfNotExistsCondition() : AccessCondition.generateEmptyCondition();
        blob.upload(inputStream, blobSize, accessCondition, null, client.v2().get());
    } catch (final StorageException se) {
        if (failIfAlreadyExists && se.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT &&
            StorageErrorCodeStrings.BLOB_ALREADY_EXISTS.equals(se.getErrorCode())) {
            throw new FileAlreadyExistsException(blobName, null, se.getMessage());
        }
        throw se;
    }
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {}) - done", blobName, blobSize));
}
 
Example #2
Source File: CloudBlobContainer.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the container if it exists using the specified request options and operation context.
 * 
 * @param accessCondition
 *            An {@link AccessCondition} object that represents the access conditions for the container.
 * @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.
 * 
 * @return <code>true</code> if the container existed and was deleted; otherwise, <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 */
@DoesServiceRequest
public boolean deleteIfExists(AccessCondition accessCondition, BlobRequestOptions options,
        OperationContext opContext) throws StorageException {
    options = BlobRequestOptions.populateAndApplyDefaults(options, BlobType.UNSPECIFIED, this.blobServiceClient);

    boolean exists = this.exists(true /* primaryOnly */, accessCondition, options, opContext);
    if (exists) {
        try {
            this.delete(accessCondition, options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.CONTAINER_NOT_FOUND.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
    else {
        return false;
    }
}
 
Example #3
Source File: CloudBlob.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #4
Source File: CloudFileDirectory.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the directory if it exists using the specified request options and operation context.
 * 
 * @param accessCondition
 *            An {@link AccessCondition} object that represents the access conditions for the directory.
 * @param options
 *            A {@link FileRequestOptions} 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 CloudFileClient}).
 * @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.
 * 
 * @return <code>true</code> if the directory existed and was deleted; otherwise, <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 * @throws URISyntaxException 
 */
@DoesServiceRequest
public boolean deleteIfExists(AccessCondition accessCondition, FileRequestOptions options,
        OperationContext opContext) throws StorageException, URISyntaxException {
    options = FileRequestOptions.populateAndApplyDefaults(options, this.fileServiceClient);

    boolean exists = this.exists(true /* primaryOnly */, accessCondition, options, opContext);
    if (exists) {
        try {
            this.delete(accessCondition, options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
    else {
        return false;
    }
}
 
Example #5
Source File: CloudFileShare.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the share if it does not exist, using the specified request options and operation context.
 * 
 * @param options
 *            A {@link FileRequestOptions} 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 CloudFileClient}).
 * @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.
 * 
 * @return <code>true</code> if the share did not already exist and was created; otherwise, <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 */
@DoesServiceRequest
public boolean createIfNotExists(FileRequestOptions options, OperationContext opContext) throws StorageException {
    options = FileRequestOptions.populateAndApplyDefaults(options, this.fileServiceClient);

    boolean exists = this.exists(true /* primaryOnly */, null /* accessCondition */, options, opContext);
    if (exists) {
        return false;
    }
    else {
        try {
            this.create(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
                    && StorageErrorCodeStrings.SHARE_ALREADY_EXISTS.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
}
 
Example #6
Source File: TableBatchOperationTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBatchInsertFail() throws StorageException {
    TableRequestOptions options = new TableRequestOptions();
    options.setTablePayloadFormat(TablePayloadFormat.Json);

    // insert entity
    Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata");
    this.table.execute(TableOperation.insert(ref), options, null);
    try {
        TableBatchOperation batch = new TableBatchOperation();
        batch.insert(ref);
        this.table.execute(batch, options, null);
        fail();
    }
    catch (TableServiceException ex) {
        assertEquals(ex.getMessage(), "Conflict");
        assertTrue(ex.getExtendedErrorInformation().getErrorMessage()
                .startsWith("The specified entity already exists"));
        assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.ENTITY_ALREADY_EXISTS);
    }
}
 
Example #7
Source File: CloudTable.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the table in the storage service with the specified request options and operation context, if it does not
 * already exist.
 *
 * @param options
 *            A {@link TableRequestOptions} 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 CloudTableClient}).
 * @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.
 *
 * @return <code>true</code> if the table did not already exist and was created; otherwise <code>false</code> .
 *
 * @throws StorageException
 *             If a storage service error occurred during the operation.
 */
@DoesServiceRequest
public boolean createIfNotExists(TableRequestOptions options, OperationContext opContext) throws StorageException {
    options = TableRequestOptions.populateAndApplyDefaults(options, this.tableServiceClient);

    boolean exists = this.exists(true, options, opContext);
    if (exists) {
        return false;
    }
    else {
        try {
            this.create(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
                    && StorageErrorCodeStrings.TABLE_ALREADY_EXISTS.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
}
 
Example #8
Source File: CloudTable.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the table from the storage service using the specified request options and operation context, if it
 * exists.
 *
 * @param options
 *            A {@link TableRequestOptions} 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 CloudTableClient}).
 * @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.
 *
 * @return A value of <code>true</code> if the table existed in the storage service and has been deleted, otherwise
 *         <code>false</code>.
 *
 * @throws StorageException
 *             If a storage service error occurred during the operation.
 */
@DoesServiceRequest
public boolean deleteIfExists(TableRequestOptions options, OperationContext opContext) throws StorageException {
    options = TableRequestOptions.populateAndApplyDefaults(options, this.tableServiceClient);

    if (this.exists(true, options, opContext)) {
        try {
            this.delete(options, opContext);
        }
        catch (StorageException ex) {
            if (ex.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.RESOURCE_NOT_FOUND.equals(ex.getErrorCode())) {
                return false;
            }
            else {
                throw ex;
            }
        }
        return true;
    }
    else {
        return false;
    }
}
 
Example #9
Source File: CloudQueue.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the queue if it does not already exist, using the specified request options and operation context.
 * 
 * @param options
 *            A {@link QueueRequestOptions} 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 CloudQueueClient}).
 * @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.
 * 
 * @return A value of <code>true</code> if the queue is created in the storage service, otherwise <code>false</code>
 *         .
 * 
 * @throws StorageException
 *             If a storage service error occurred during the operation.
 */
@DoesServiceRequest
public boolean createIfNotExists(QueueRequestOptions options, OperationContext opContext) throws StorageException {
    options = QueueRequestOptions.populateAndApplyDefaults(options, this.queueServiceClient);

    boolean exists = this.exists(true, options, opContext);
    if (exists) {
        return false;
    }
    else {
        try {
            this.create(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
                    && StorageErrorCodeStrings.QUEUE_ALREADY_EXISTS.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
}
 
Example #10
Source File: TableBatchOperationTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBatchOver100Entities() throws StorageException {
    TableRequestOptions options = new TableRequestOptions();
    options.setTablePayloadFormat(TablePayloadFormat.Json);

    TableBatchOperation batch = new TableBatchOperation();
    try {
        for (int m = 0; m < 101; m++) {
            batch.insert(TableTestHelper.generateRandomEntity("jxscl_odata"));
        }

        this.table.execute(batch, options, null);
        fail("Batch with over 100 entities should fail.");
    }
    catch (TableServiceException ex) {
        assertEquals(ex.getMessage(), "Bad Request");
        String errorAfterSemiColon = ex.getExtendedErrorInformation().getErrorMessage();
        errorAfterSemiColon = errorAfterSemiColon.substring(errorAfterSemiColon.indexOf(":") + 1);
        assertTrue(errorAfterSemiColon.startsWith("The batch request operation exceeds the maximum 100 changes per change set."));
        assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.INVALID_INPUT);
    }
}
 
Example #11
Source File: CloudFileShareTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteShareSnapshotOptions() throws StorageException, URISyntaxException, IOException {
    // create share with metadata
    this.share.create();
    assertTrue(this.share.exists());

    // verify that exists() call on snapshot populates metadata
    CloudFileShare snapshot = this.share.createSnapshot();
    CloudFileClient client = FileTestHelper.createCloudFileClient();
    CloudFileShare snapshotRef = client.getShareReference(snapshot.name, snapshot.snapshotID);
    assertTrue(snapshotRef.exists());

    try {
        share.delete();
    }
    catch (final StorageException e) {
        assertEquals(StorageErrorCodeStrings.SHARE_HAS_SNAPSHOTS, e.getErrorCode());
    }
    
    share.delete(DeleteShareSnapshotsOption.INCLUDE_SNAPSHOTS, null, null, null);
    assertFalse(share.exists());
    assertFalse(snapshot.exists());
}
 
Example #12
Source File: CloudQueue.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the queue if it exists, using the specified request options and operation context.
 * 
 * @param options
 *            A {@link QueueRequestOptions} 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 CloudQueueClient}).
 * @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.
 * 
 * @return A value of <code>true</code> if the queue existed in the storage service and has been deleted, otherwise
 *         <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred during the operation.
 */
@DoesServiceRequest
public boolean deleteIfExists(QueueRequestOptions options, OperationContext opContext) throws StorageException {
    options = QueueRequestOptions.populateAndApplyDefaults(options, this.queueServiceClient);

    boolean exists = this.exists(true, options, opContext);
    if (exists) {
        try {
            this.delete(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.QUEUE_NOT_FOUND.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }

    }
    else {
        return false;
    }
}
 
Example #13
Source File: LeaseTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobLeaseAcquireAndRelease() throws URISyntaxException, StorageException, IOException {
    final int length = 128;
    final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null);

    // Get Lease 
    OperationContext operationContext = new OperationContext();
    final String leaseID = blobRef.acquireLease(15, null /*proposed lease id */, null /*access condition*/,
            null/* BlobRequestOptions */, operationContext);
    final AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID);
    assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_CREATED);

    tryUploadWithBadLease(length, blobRef, null, StorageErrorCodeStrings.LEASE_ID_MISSING);

    // Try to upload with lease
    blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1, leaseCondition, null, null);

    // Release lease
    blobRef.releaseLease(leaseCondition);

    // now upload with no lease specified.
    blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1);
}
 
Example #14
Source File: AzureStorageQueueService.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * This method create a queue if it doesn't exist
 */
public boolean createQueueIfNotExists(String queueName) throws InvalidKeyException, URISyntaxException, StorageException {
    CloudQueueClient client = connection.getCloudStorageAccount().createCloudQueueClient();
    CloudQueue queueRef = client.getQueueReference(queueName);
    boolean creationResult;
    try {
        creationResult = queueRef.createIfNotExists(null, AzureStorageUtils.getTalendOperationContext());
    } catch (StorageException e) {
        if (!e.getErrorCode().equals(StorageErrorCodeStrings.QUEUE_BEING_DELETED)) {
            throw e;
        }
        LOGGER.warn(messages.getMessage("error.QueueDeleted", queueRef.getName()));
        // Documentation doesn't specify how many seconds at least to wait.
        // 40 seconds before retrying.
        // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delete-queue3
        try {
            Thread.sleep(40000);
        } catch (InterruptedException eint) {
            throw new RuntimeException(messages.getMessage("error.InterruptedException"));
        }
        creationResult = queueRef.createIfNotExists(null, AzureStorageUtils.getTalendOperationContext());
        LOGGER.debug(messages.getMessage("debug.QueueCreated", queueRef.getName()));
    }

    return creationResult;
}
 
Example #15
Source File: AzureStorageTableService.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * This method create a table after it's deletion.<br/>
 * the table deletion take about 40 seconds to be effective on azure CF.
 * https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Delete-Table#Remarks <br/>
 * So we try to wait 50 seconds if the first table creation return an
 * {@link StorageErrorCodeStrings.TABLE_BEING_DELETED } exception code
 * 
 * @param cloudTable
 * 
 * @throws StorageException
 * @throws IOException
 * 
 */
private void createTableAfterDeletion(CloudTable cloudTable) throws StorageException, IOException {
    try {
        cloudTable.create(null, AzureStorageUtils.getTalendOperationContext());
    } catch (TableServiceException e) {
        if (!e.getErrorCode().equals(StorageErrorCodeStrings.TABLE_BEING_DELETED)) {
            throw e;
        }
        LOGGER.warn("Table '{}' is currently being deleted. We'll retry in a few moments...", cloudTable.getName());
        // wait 50 seconds (min is 40s) before retrying.
        // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/Delete-Table#Remarks
        try {
            Thread.sleep(50000);
        } catch (InterruptedException eint) {
            throw new IOException("Wait process for recreating table interrupted.");
        }
        cloudTable.create(null, AzureStorageUtils.getTalendOperationContext());
        LOGGER.debug("Table {} created.", cloudTable.getName());
    }
}
 
Example #16
Source File: Utility.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a value representing the remaining time before the operation expires.
 * 
 * @param operationExpiryTimeInMs
 *            the time the request expires
 * @param timeoutIntervalInMs
 *            the server side timeout interval
 * @return the remaining time before the operation expires
 * @throws StorageException
 *             wraps a TimeoutException if there is no more time remaining
 */
public static int getRemainingTimeout(Long operationExpiryTimeInMs, Integer timeoutIntervalInMs) throws StorageException {
    if (operationExpiryTimeInMs != null) {
        long remainingTime = operationExpiryTimeInMs - new Date().getTime();
        if (remainingTime > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }
        else if (remainingTime > 0) {
            return (int) remainingTime;
        }
        else {
            TimeoutException timeoutException = new TimeoutException(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION);
            StorageException translatedException = new StorageException(
                    StorageErrorCodeStrings.OPERATION_TIMED_OUT, SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION,
                    Constants.HeaderConstants.HTTP_UNUSED_306, null, timeoutException);
            throw translatedException;
        }
    }
    else if (timeoutIntervalInMs != null) {
        return timeoutIntervalInMs + Constants.DEFAULT_READ_TIMEOUT;
    }
    else {
        return Constants.DEFAULT_READ_TIMEOUT;
    }
}
 
Example #17
Source File: CloudFile.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the file if it exists, using the specified access condition, request options, and operation context.
 * 
 * @param accessCondition
 *            An {@link AccessCondition} object that represents the access conditions for the file.
 * @param options
 *            A {@link FileRequestOptions} 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 CloudFileClient}).
 * @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.
 * 
 * @return <code>true</code> if the file existed and was deleted; otherwise, <code>false</code>
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 * @throws URISyntaxException 
 */
@DoesServiceRequest
public final boolean deleteIfExists(final AccessCondition accessCondition, FileRequestOptions options,
        OperationContext opContext) throws StorageException, URISyntaxException {
    options = FileRequestOptions.populateAndApplyDefaults(options, this.fileServiceClient);
    this.getShare().assertNoSnapshot();

    boolean exists = this.exists(true, accessCondition, options, opContext);
    if (exists) {
        try {
            this.delete(accessCondition, options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }

    }
    else {
        return false;
    }
}
 
Example #18
Source File: CloudFileDirectory.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the directory if it does not exist, using the specified request options and operation context.
 * 
 * @param options
 *            A {@link FileRequestOptions} 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 CloudFileClient}).
 * @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.
 * 
 * @return <code>true</code> if the directory did not already exist and was created; otherwise, <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 * @throws URISyntaxException 
 */
@DoesServiceRequest
public boolean createIfNotExists(FileRequestOptions options, OperationContext opContext) throws StorageException, URISyntaxException {
    options = FileRequestOptions.populateAndApplyDefaults(options, this.fileServiceClient);

    this.getShare().assertNoSnapshot();

    boolean exists = this.exists(true /* primaryOnly */, null /* accessCondition */, options, opContext);
    if (exists) {
        return false;
    }
    else {
        try {
            this.create(options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
                    && StorageErrorCodeStrings.RESOURCE_ALREADY_EXISTS.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
}
 
Example #19
Source File: CloudBlob.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the blob if it exists, using the specified snapshot and request options, and operation context.
 * <p>
 * A blob that has snapshots cannot be deleted unless the snapshots are also deleted. If a blob has snapshots, use
 * the {@link DeleteSnapshotsOption#DELETE_SNAPSHOTS_ONLY} or {@link DeleteSnapshotsOption#INCLUDE_SNAPSHOTS} value
 * in the <code>deleteSnapshotsOption</code> parameter to specify how the snapshots should be handled when the blob
 * is deleted.
 *
 * @param deleteSnapshotsOption
 *            A {@link DeleteSnapshotsOption} object that indicates whether to delete only snapshots, or the blob
 *            and its snapshots.
 * @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.
 *
 * @return <code>true</code> if the blob existed and was deleted; otherwise, <code>false</code>
 *
 * @throws StorageException
 *             If a storage service error occurred.
 */
@DoesServiceRequest
public final boolean deleteIfExists(final DeleteSnapshotsOption deleteSnapshotsOption,
        final AccessCondition accessCondition, BlobRequestOptions options, OperationContext opContext)
        throws StorageException {
    options = BlobRequestOptions.populateAndApplyDefaults(options, this.properties.getBlobType(), this.blobServiceClient);

    boolean exists = this.exists(true, accessCondition, options, opContext);
    if (exists) {
        try {
            this.delete(deleteSnapshotsOption, accessCondition, options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
                    && StorageErrorCodeStrings.BLOB_NOT_FOUND.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }

    }
    else {
        return false;
    }
}
 
Example #20
Source File: CloudBlobContainer.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the container if it does not exist, using the specified request options and operation context.
 * 
 * @param accessType
 *            A {@link BlobContainerPublicAccessType} object that specifies whether data in the container may be
 *            accessed publicly and what level of access is to be allowed.
 * @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.
 * 
 * @return <code>true</code> if the container did not already exist and was created; otherwise, <code>false</code>.
 * 
 * @throws StorageException
 *             If a storage service error occurred.
 */
@DoesServiceRequest
public boolean createIfNotExists(BlobContainerPublicAccessType accessType, BlobRequestOptions options, OperationContext opContext) throws StorageException {
    if (accessType == BlobContainerPublicAccessType.UNKNOWN) {
        throw new IllegalArgumentException(String.format(Utility.LOCALE_US, SR.ARGUMENT_OUT_OF_RANGE_ERROR, "accessType", accessType));
    }

    options = BlobRequestOptions.populateAndApplyDefaults(options, BlobType.UNSPECIFIED, this.blobServiceClient);

    boolean exists = this.exists(true /* primaryOnly */, null /* accessCondition */, options, opContext);
    if (exists) {
        return false;
    }
    else {
        try {
            this.create(accessType, options, opContext);
            return true;
        }
        catch (StorageException e) {
            if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
                    && StorageErrorCodeStrings.CONTAINER_ALREADY_EXISTS.equals(e.getErrorCode())) {
                return false;
            }
            else {
                throw e;
            }
        }
    }
}
 
Example #21
Source File: CloudQueueTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testDeleteQueueIfExists() throws URISyntaxException, StorageException {
    final CloudQueue queue = QueueTestHelper.getRandomQueueReference();

    assertFalse(queue.deleteIfExists());

    try {
        final OperationContext createQueueContext = new OperationContext();
        queue.create(null, createQueueContext);
        assertEquals(createQueueContext.getLastResult().getStatusCode(), HttpURLConnection.HTTP_CREATED);

        assertTrue(queue.deleteIfExists());
        assertFalse(queue.deleteIfExists());

        try {
            queue.create();
            fail("Queue CreateIfNotExists did not throw exception while trying to create a queue in BeingDeleted State");
        }
        catch (StorageException ex) {
            assertEquals("Expected 409 Exception, QueueBeingDeleted not thrown", ex.getHttpStatusCode(),
                    HttpURLConnection.HTTP_CONFLICT);
            assertEquals("Expected 409 Exception, QueueBeingDeleted not thrown", ex.getExtendedErrorInformation()
                    .getErrorCode(), StorageErrorCodeStrings.QUEUE_BEING_DELETED);
        }
    }
    finally {
        queue.delete();
    }
}
 
Example #22
Source File: CloudQueueTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testQueueCreateIfNotExistsAfterDelete() throws URISyntaxException, StorageException {
    final CloudQueueClient qClient = TestHelper.createCloudQueueClient();
    final String queueName = QueueTestHelper.generateRandomQueueName();
    CloudQueue queue = qClient.getQueueReference(queueName);
    assertEquals(queueName, queue.getName());

    try {

        OperationContext createQueueContext1 = new OperationContext();
        assertTrue(queue.createIfNotExists(null, createQueueContext1));
        assertEquals(createQueueContext1.getLastResult().getStatusCode(), HttpURLConnection.HTTP_CREATED);

        assertTrue(queue.deleteIfExists());
        try {
            queue.createIfNotExists();
            fail("Queue CreateIfNotExists did not throw exception while trying to create a queue in BeingDeleted State");
        }
        catch (StorageException ex) {
            assertEquals("Expected 409 Exception, QueueBeingDeleted not thrown", ex.getHttpStatusCode(),
                    HttpURLConnection.HTTP_CONFLICT);
            assertEquals("Expected 409 Exception, QueueBeingDeleted not thrown", ex.getExtendedErrorInformation()
                    .getErrorCode(), StorageErrorCodeStrings.QUEUE_BEING_DELETED);
        }
    }
    finally {
        queue.deleteIfExists();
    }
}
 
Example #23
Source File: CloudQueueTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testQueueCreateAfterDelete() throws URISyntaxException, StorageException {
    final CloudQueueClient qClient = TestHelper.createCloudQueueClient();
    final String queueName = QueueTestHelper.generateRandomQueueName();
    CloudQueue queue = qClient.getQueueReference(queueName);
    assertEquals(queueName, queue.getName());

    try {
        OperationContext createQueueContext1 = new OperationContext();
        assertTrue(queue.createIfNotExists(null, createQueueContext1));
        assertEquals(createQueueContext1.getLastResult().getStatusCode(), HttpURLConnection.HTTP_CREATED);

        assertTrue(queue.deleteIfExists());
        try {
            queue.create();
            fail("Queue CreateIfNotExists did not throw exception while trying to create a queue in BeingDeleted State");
        }
        catch (StorageException ex) {
            assertEquals("Expected 409 Exception, QueueBeingDeleted not thrown", ex.getHttpStatusCode(),
                    HttpURLConnection.HTTP_CONFLICT);
            assertEquals("Expected 409 Exception, QueueBeingDeleted not thrown", ex.getExtendedErrorInformation()
                    .getErrorCode(), StorageErrorCodeStrings.QUEUE_BEING_DELETED);
        }
    }
    finally {
        queue.deleteIfExists();
    }
}
 
Example #24
Source File: TableBatchOperationTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchMergeFail() throws StorageException {
    TableRequestOptions options = new TableRequestOptions();
    options.setTablePayloadFormat(TablePayloadFormat.Json);

    TableBatchOperation batch = new TableBatchOperation();
    addInsertBatch(batch);

    // Insert entity to merge
    Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata");
    this.table.execute(TableOperation.insert(baseEntity), options, null);

    Class1 updatedEntity = TableTestHelper.generateRandomEntity("jxscl_odata");
    updatedEntity.setPartitionKey(baseEntity.getPartitionKey());
    updatedEntity.setRowKey(baseEntity.getRowKey());
    updatedEntity.setEtag(baseEntity.getEtag());
    this.table.execute(TableOperation.replace(updatedEntity), options, null);

    // add merge to fail
    addMergeToBatch(baseEntity, batch);

    try {
        this.table.execute(batch, options, null);
        fail();
    }
    catch (TableServiceException ex) {
        assertEquals(ex.getMessage(), "Precondition Failed");
        String errorAfterSemiColon = ex.getExtendedErrorInformation().getErrorMessage();
        errorAfterSemiColon = errorAfterSemiColon.substring(errorAfterSemiColon.indexOf(":") + 1);
        assertTrue(errorAfterSemiColon
                .startsWith("The update condition specified in the request was not satisfied."));
        assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.UPDATE_CONDITION_NOT_SATISFIED);
    }
}
 
Example #25
Source File: TableBatchOperationTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchReplaceFail() throws StorageException {
    TableRequestOptions options = new TableRequestOptions();
    options.setTablePayloadFormat(TablePayloadFormat.Json);

    TableBatchOperation batch = new TableBatchOperation();

    // Insert entity to merge
    Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata");
    this.table.execute(TableOperation.insert(baseEntity), options, null);

    Class1 updatedEntity = TableTestHelper.generateRandomEntity("jxscl_odata");
    updatedEntity.setPartitionKey(baseEntity.getPartitionKey());
    updatedEntity.setRowKey(baseEntity.getRowKey());
    updatedEntity.setEtag(baseEntity.getEtag());
    this.table.execute(TableOperation.replace(updatedEntity), options, null);

    // add merge to fail
    addReplaceToBatch(baseEntity, batch);

    try {
        this.table.execute(batch);
        fail();
    }
    catch (TableServiceException ex) {
        assertEquals(ex.getMessage(), "Precondition Failed");
        assertTrue(ex.getExtendedErrorInformation().getErrorMessage()
                .startsWith("The update condition specified in the request was not satisfied."));
        assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.UPDATE_CONDITION_NOT_SATISFIED);
    }
}
 
Example #26
Source File: TableBatchOperationTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchDeleteFail() throws StorageException {
    TableRequestOptions options = new TableRequestOptions();
    options.setTablePayloadFormat(TablePayloadFormat.Json);

    TableBatchOperation batch = new TableBatchOperation();

    // Insert entity to delete
    Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata");
    this.table.execute(TableOperation.insert(baseEntity), options, null);

    Class1 updatedEntity = TableTestHelper.generateRandomEntity("jxscl_odata");
    updatedEntity.setPartitionKey(baseEntity.getPartitionKey());
    updatedEntity.setRowKey(baseEntity.getRowKey());
    updatedEntity.setEtag(baseEntity.getEtag());
    this.table.execute(TableOperation.replace(updatedEntity), options, null);

    // add delete to fail
    batch.delete(baseEntity);

    try {
        this.table.execute(batch, options, null);
        fail();
    }
    catch (TableServiceException ex) {
        assertEquals(ex.getMessage(), "Precondition Failed");
        assertTrue(ex.getExtendedErrorInformation().getErrorMessage()
                .startsWith("The update condition specified in the request was not satisfied."));
        assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.UPDATE_CONDITION_NOT_SATISFIED);
    }
}
 
Example #27
Source File: TableBatchOperationTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchSizeOver4mb() {
    TableRequestOptions options = new TableRequestOptions();
    options.setTablePayloadFormat(TablePayloadFormat.Json);

    TableBatchOperation batch = new TableBatchOperation();
    byte[] datArr = new byte[1024 * 128];
    Random rand = new Random();
    rand.nextBytes(datArr);

    // Each entity is approx 128kb, meaning ~32 entities will result in a request over 4mb.
    try {
        for (int m = 0; m < 32; m++) {
            Class1 ref = new Class1();

            ref.setA("foo_A");
            ref.setB("foo_B");
            ref.setC("foo_C");
            ref.setD(datArr);
            ref.setPartitionKey("jxscl_odata");
            ref.setRowKey(UUID.randomUUID().toString());
            batch.insert(ref);
        }

        this.table.execute(batch, options, null);
        fail();
    }
    catch (StorageException ex) {
        assertEquals(ex.getHttpStatusCode(), HttpURLConnection.HTTP_ENTITY_TOO_LARGE);
        assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.REQUEST_BODY_TOO_LARGE);
        assertTrue(ex.getMessage().startsWith(
                "The request body is too large and exceeds the maximum permissible limit."));
    }
}
 
Example #28
Source File: TableBatchOperationTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchInsertEntityOver1MB() throws StorageException {
    TableRequestOptions options = new TableRequestOptions();
    options.setTablePayloadFormat(TablePayloadFormat.Json);

    TableBatchOperation batch = new TableBatchOperation();
    Class1 bigEnt = new Class1();

    bigEnt.setA("foo_A");
    bigEnt.setB("foo_B");
    bigEnt.setC("foo_C");
    // 1mb right here
    bigEnt.setD(new byte[1024 * 1024]);
    bigEnt.setPartitionKey("jxscl_odata");
    bigEnt.setRowKey(UUID.randomUUID().toString());

    batch.insert(bigEnt);

    for (int m = 0; m < 3; m++) {
        Class1 ref = new Class1();
        ref.setA("foo_A");
        ref.setB("foo_B");
        ref.setC("foo_C");
        ref.setPartitionKey("jxscl_odata");
        ref.setRowKey(UUID.randomUUID().toString());
        batch.insert(ref);
    }

    try {
        this.table.execute(batch, options, null);
        fail();
    }
    catch (TableServiceException ex) {
        assertEquals(ex.getMessage(), "Bad Request");
        String errorAfterSemiColon = ex.getExtendedErrorInformation().getErrorMessage();
        errorAfterSemiColon = errorAfterSemiColon.substring(errorAfterSemiColon.indexOf(":") + 1);
        assertTrue(errorAfterSemiColon.startsWith("The entity is larger than the maximum allowed size (1MB)."));
        assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.ENTITY_TOO_LARGE);
    }
}
 
Example #29
Source File: AzureStorageBlobService.java    From components with Apache License 2.0 5 votes vote down vote up
/**
 * This method create an azure container if it doesn't exist and set it access policy
 *
 * @param containerName : the name of the container to be created
 * @return true if the container was created, false otherwise
 */
public boolean createContainerIfNotExist(final String containerName, final BlobContainerPublicAccessType accessType)
        throws StorageException, URISyntaxException, InvalidKeyException {
    CloudBlobClient cloudBlobClient = connection.getCloudStorageAccount().createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);

    boolean containerCreated;
    try {
        containerCreated = cloudBlobContainer
                .createIfNotExists(accessType, null, AzureStorageUtils.getTalendOperationContext());
    } catch (StorageException e) {
        if (!e.getErrorCode().equals(StorageErrorCodeStrings.CONTAINER_BEING_DELETED)) {
            throw e;
        }
        LOGGER.warn(messages.getMessage("error.CONTAINER_BEING_DELETED", containerName));
        // wait 40 seconds (min is 30s) before retrying.
        // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delete-container
        try {
            Thread.sleep(40000);
        } catch (InterruptedException eint) {
            LOGGER.error(messages.getMessage("error.InterruptedException"));
            throw new ComponentException(eint);
        }
        containerCreated = cloudBlobContainer
                .createIfNotExists(accessType, null, AzureStorageUtils.getTalendOperationContext());
        LOGGER.debug(messages.getMessage("debug.ContainerCreated", containerName));
    }

    return containerCreated;
}
 
Example #30
Source File: TestBlobDataValidation.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
  assumeNotNull(testAccount);
  // Write a test file.
  String testFileKey = "testFile";
  Path testFilePath = new Path("/" + testFileKey);
  OutputStream outStream = testAccount.getFileSystem().create(testFilePath);
  outStream.write(new byte[] { 5, 15 });
  outStream.close();

  // Check that we stored/didn't store the MD5 field as configured.
  CloudBlockBlob blob = testAccount.getBlobReference(testFileKey);
  blob.downloadAttributes();
  String obtainedMd5 = blob.getProperties().getContentMD5();
  if (expectMd5Stored) {
    assertNotNull(obtainedMd5);
  } else {
    assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
  }

  // Mess with the content so it doesn't match the MD5.
  String newBlockId = Base64.encode(new byte[] { 55, 44, 33, 22 });
  blob.uploadBlock(newBlockId,
      new ByteArrayInputStream(new byte[] { 6, 45 }), 2);
  blob.commitBlockList(Arrays.asList(new BlockEntry[] { new BlockEntry(
      newBlockId, BlockSearchMode.UNCOMMITTED) }));

  // Now read back the content. If we stored the MD5 for the blob content
  // we should get a data corruption error.
  InputStream inStream = testAccount.getFileSystem().open(testFilePath);
  try {
    byte[] inBuf = new byte[100];
    while (inStream.read(inBuf) > 0){
      //nothing;
    }
    inStream.close();
    if (expectMd5Stored) {
      fail("Should've thrown because of data corruption.");
    }
  } catch (IOException ex) {
    if (!expectMd5Stored) {
      throw ex;
    }
    StorageException cause = (StorageException)ex.getCause();
    assertNotNull(cause);
    assertTrue("Unexpected cause: " + cause,
        cause.getErrorCode().equals(StorageErrorCodeStrings.INVALID_MD5));
  }
}