Search in sources :

Example 1 with SharedAccessAccountPolicy

use of com.microsoft.azure.storage.SharedAccessAccountPolicy in project beam by apache.

the class AzureBlobStoreFileSystem method generateSasToken.

@VisibleForTesting
/**
 * Generate an SAS Token if the user did not provide one through pipeline options
 */
String generateSasToken() throws IOException {
    if (!Strings.isNullOrEmpty(options.getSasToken())) {
        return options.getSasToken();
    }
    SharedAccessAccountPolicy sharedAccessAccountPolicy = new SharedAccessAccountPolicy();
    long date = new Date().getTime();
    long expiryDate = new Date(date + DEFAULT_EXPIRY_TIME).getTime();
    sharedAccessAccountPolicy.setPermissionsFromString(DEFAULT_PERMISSIONS);
    sharedAccessAccountPolicy.setSharedAccessStartTime(new Date(date));
    sharedAccessAccountPolicy.setSharedAccessExpiryTime(new Date(expiryDate));
    sharedAccessAccountPolicy.setResourceTypeFromString(DEFAULT_RESOURCE_TYPES);
    sharedAccessAccountPolicy.setServiceFromString(DEFAULT_SERVICES);
    String storageConnectionString;
    if (!Strings.isNullOrEmpty(options.getAzureConnectionString())) {
        storageConnectionString = options.getAzureConnectionString();
    } else if (!Strings.isNullOrEmpty(options.getAccessKey())) {
        storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=" + client.get().getAccountName() + ";AccountKey=" + options.getAccessKey() + ";EndpointSuffix=core.windows.net";
    } else {
        throw new IOException("Copying blobs requires that a SAS token, connection string, or account key be provided.");
    }
    try {
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        return "?" + storageAccount.generateSharedAccessSignature(sharedAccessAccountPolicy);
    } catch (Exception e) {
        throw (IOException) e.getCause();
    }
}
Also used : SharedAccessAccountPolicy(com.microsoft.azure.storage.SharedAccessAccountPolicy) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) IOException(java.io.IOException) Date(java.util.Date) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 2 with SharedAccessAccountPolicy

use of com.microsoft.azure.storage.SharedAccessAccountPolicy in project hadoop by apache.

the class LocalSASKeyGeneratorImpl method getDefaultAccountAccessPolicy.

/**
   * Helper method to generate Access Policy for the Storage Account SAS Key
   * @return SharedAccessAccountPolicy
   */
private SharedAccessAccountPolicy getDefaultAccountAccessPolicy() {
    SharedAccessAccountPolicy ap = new SharedAccessAccountPolicy();
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    cal.setTime(new Date());
    cal.add(Calendar.HOUR, (int) getSasKeyExpiryPeriod() * HOURS_IN_DAY);
    ap.setSharedAccessExpiryTime(cal.getTime());
    ap.setPermissions(getDefaultAccoutSASKeyPermissions());
    ap.setResourceTypes(EnumSet.of(SharedAccessAccountResourceType.CONTAINER, SharedAccessAccountResourceType.OBJECT));
    ap.setServices(EnumSet.of(SharedAccessAccountService.BLOB));
    return ap;
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) SharedAccessAccountPolicy(com.microsoft.azure.storage.SharedAccessAccountPolicy) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date)

Aggregations

SharedAccessAccountPolicy (com.microsoft.azure.storage.SharedAccessAccountPolicy)2 Date (java.util.Date)2 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)1 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)1