/** * Gets a {@link CloudFileShare} object with the specified name. * * @param shareName * The name of the share, which must adhere to share naming rules. The share name should not * include any path separator characters (/). * Share names must be lowercase, between 3-63 characters long and must start with a letter or * number. Share names may contain only letters, numbers, and the dash (-) character. * * @return A reference to a {@link CloudFileShare} object. * @throws StorageException * @throws URISyntaxException * * @see <a href="http://msdn.microsoft.com/en-us/library/azure/dn167011.aspx">Naming and Referencing Shares, * Directories, Files, and Metadata</a> */ public CloudFileShare getShareReference(final String shareName) throws URISyntaxException, StorageException { Utility.assertNotNullOrEmpty("shareName", shareName); return this.getShareReference(shareName, null); }
/** * Creates a new File service client. * * @return A {@link CloudFileClient} that represents the cloud File client. * */ public CloudFileClient createCloudFileClient() { if (this.getFileStorageUri() == null) { throw new IllegalArgumentException(SR.FILE_ENDPOINT_NOT_CONFIGURED); } if (this.credentials == null) { throw new IllegalArgumentException(SR.MISSING_CREDENTIALS); } if (!StorageCredentialsHelper.canCredentialsGenerateClient(this.credentials)) { throw new IllegalArgumentException(SR.CREDENTIALS_CANNOT_SIGN_REQUEST); } return new CloudFileClient(this.getFileStorageUri(), this.getCredentials()); }
/** * Retrieves the current {@link FileServiceProperties} for the given storage service. This encapsulates * the CORS configurations. * * @return A {@link FileServiceProperties} object representing the current configuration of the service. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public final FileServiceProperties downloadServiceProperties() throws StorageException { return this.downloadServiceProperties(null /* options */, null /* opContext */); }
/** * Returns the share for this directory. * * @return A {@link CloudFileShare} that represents the share for this directory. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ @Override public CloudFileShare getShare() throws StorageException, URISyntaxException { if (this.share == null) { this.share = this.fileServiceClient.getShareReference(PathUtility.getShareNameFromUri(this.getUri(), this.fileServiceClient.isUsePathStyleUris())); } return this.share; }
/** * Returns the canonical name of the file in the format of * <i>/<service-name>/<account-name>/<share-name>/<file-name></i>. * <p> * This format is used for Shared Access operations. * * @return The canonical name in the format of <i>/<service-name>/<account-name> * /<share-name>/<file-name></i>. */ String getCanonicalName() { StringBuilder canonicalName = new StringBuilder("/"); canonicalName.append(SR.FILE); String rawPath = this.getUri().getRawPath(); if (this.fileServiceClient.isUsePathStyleUris()) { canonicalName.append(rawPath); } else { canonicalName.append(PathUtility.getCanonicalPathFromCredentials( this.getServiceClient().getCredentials(), rawPath)); } return canonicalName.toString(); }
@Override public HttpURLConnection buildRequest(CloudFileClient client, Void parentObject, OperationContext context) throws Exception { listingContext.setMarker(segmentedRequest.getToken() != null ? segmentedRequest.getToken() .getNextMarker() : null); return FileRequest.listShares( client.getCredentials().transformUri(client.getStorageUri()).getUri(this.getCurrentLocation()), options, context, listingContext, detailsIncluded); }
@Test public void testFileSAS() throws InvalidKeyException, IllegalArgumentException, StorageException, URISyntaxException, InterruptedException { SharedAccessFilePolicy policy = createSharedAccessPolicy( EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.LIST), 300); FileSharePermissions perms = new FileSharePermissions(); perms.getSharedAccessPolicies().put("readperm", policy); this.share.uploadPermissions(perms); Thread.sleep(30000); CloudFile sasFile = new CloudFile( new URI(this.file.getUri().toString() + "?" + this.file.generateSharedAccessSignature(null, "readperm"))); sasFile.download(new ByteArrayOutputStream()); // do not give the client and check that the new file's client has the correct permissions CloudFile fileFromUri = new CloudFile(PathUtility.addToQuery(this.file.getStorageUri(), this.file.generateSharedAccessSignature(null, "readperm"))); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), fileFromUri.getServiceClient().getCredentials().getClass().toString()); // create credentials from sas StorageCredentials creds = new StorageCredentialsSharedAccessSignature( this.file.generateSharedAccessSignature(policy, null, null)); CloudFileClient client = new CloudFileClient(sasFile.getServiceClient().getStorageUri(), creds); CloudFile fileFromClient = client.getShareReference(this.file.getShare().getName()).getRootDirectoryReference() .getFileReference(this.file.getName()); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), fileFromClient.getServiceClient().getCredentials().getClass().toString()); assertEquals(client, fileFromClient.getServiceClient()); }
/** * Returns the URI after applying the authentication transformation. * * @return A <code>java.net.URI</code> object that represents the URI after applying the authentication * transformation. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ private StorageUri getTransformedAddress() throws URISyntaxException, StorageException { return this.fileServiceClient.getCredentials().transformUri(this.storageUri); } }
for (int i = 0; i < 30; i++) { shareList.add(prefix + i); fileClient.getShareReference(prefix + i).create(); for (final CloudFileShare share : fileClient.listShares(prefix)) { assertEquals(CloudFileShare.class, share.getClass()); count++; ResultSegment<CloudFileShare> segment = fileClient.listSharesSegmented(prefix, EnumSet.allOf(ShareListingDetails.class), 15, token, null, null); fileClient.getShareReference(shareName).deleteIfExists();
URI sasUri = sasClient.getShareReference(share.getName()).getUri(); sasUri = sasClient.getCredentials().transformUri(sasUri); final CloudFileShare sasShare = new CloudFileShare(sasUri); (policy.getPermissions().contains(SharedAccessAccountPermissions.LIST))) { assertEquals(sasShare.getName(), sasClient.listShares(sasShare.getName()).iterator().next().getName()); sasClient.listShares(sasShare.getName()).iterator().next(); fail(); assertEquals(AccountSasTests.ENUMERATION_ERROR_MESSAGE, ex.getMessage()); assertEquals(sasShare.getName(), this.fileClient.listShares(sasShare.getName()).iterator().next().getName());
CloudFileShare snapshotRef = client.getShareReference(snapshot.name, snapshot.snapshotID); assertTrue(snapshotRef.exists()); assertTrue(snapshotRef.getMetadata().size() == 1 && snapshotRef.getMetadata().get("key1").equals("value1")); CloudFileShare snapshotRef2 = client.getShareReference(snapshot.name, snapshot.snapshotID); snapshotRef2.downloadAttributes(); snapshot.downloadAttributes(); shareMeta2.put("abc", "def"); CloudFileShare snapshotRef3 = this.share.createSnapshot(shareMeta2, null, null, null); CloudFileShare snapshotRef4 = client.getShareReference(snapshotRef3.name, snapshotRef3.snapshotID); assertTrue(snapshotRef4.exists()); assertTrue(snapshotRef4.getMetadata().size() == 1 && snapshotRef4.getMetadata().get("abc").equals("def")); uriBuilder.add("sharesnapshot", snapshot.snapshotID); CloudFileShare snapshotRef5 = new CloudFileShare(uriBuilder.addToURI(this.share.getUri()), this.share.getServiceClient().getCredentials()); assertEquals(snapshot.snapshotID, snapshotRef5.snapshotID); assertTrue(snapshotRef5.exists());
assertEquals("value1", this.share.getMetadata().get("key1")); CloudFileShare share2 = this.share.getServiceClient().getShareReference(this.share.getName()); share2.downloadAttributes(); assertEquals(1, share2.getMetadata().size()); assertEquals("value2", this.share.getMetadata().get("key2")); Iterable<CloudFileShare> shares = this.share.getServiceClient().listShares(this.share.getName(), EnumSet.of(ShareListingDetails.METADATA), null, null);
/** * Takes a CorsRule and tries to upload it. Then tries to download it and compares it to the initial CorsRule. */ private void testCorsRules(CorsRule rule, ServiceClient client, ServiceProperties properties, FileServiceProperties fileServiceProperties) throws StorageException, InterruptedException { CorsProperties cors = (fileServiceProperties == null) ? properties.getCors() : fileServiceProperties.getCors(); cors.getCorsRules().clear(); cors.getCorsRules().add(rule); if (fileServiceProperties == null) { callUploadServiceProps(client, properties, null); assertServicePropertiesAreEqual(properties, callDownloadServiceProperties(client)); } else { CloudFileClient fileClient = ((CloudFileClient) client); fileClient.uploadServiceProperties(fileServiceProperties); Thread.sleep(30000); assertFileServicePropertiesAreEqual(fileServiceProperties, fileClient.downloadServiceProperties()); } }
@Test public void testCloudStorageAccountClientMethods() throws URISyntaxException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount account = new CloudStorageAccount(cred, false); CloudBlobClient blob = account.createCloudBlobClient(); CloudQueueClient queue = account.createCloudQueueClient(); CloudTableClient table = account.createCloudTableClient(); CloudFileClient file = account.createCloudFileClient(); // check endpoints assertEquals("Blob endpoint doesn't match account", account.getBlobEndpoint(), blob.getEndpoint()); assertEquals("Queue endpoint doesn't match account", account.getQueueEndpoint(), queue.getEndpoint()); assertEquals("Table endpoint doesn't match account", account.getTableEndpoint(), table.getEndpoint()); assertEquals("File endpoint doesn't match account", account.getFileEndpoint(), file.getEndpoint()); // check storage uris assertEquals("Blob endpoint doesn't match account", account.getBlobStorageUri(), blob.getStorageUri()); assertEquals("Queue endpoint doesn't match account", account.getQueueStorageUri(), queue.getStorageUri()); assertEquals("Table endpoint doesn't match account", account.getTableStorageUri(), table.getStorageUri()); assertEquals("File endpoint doesn't match account", account.getFileStorageUri(), file.getStorageUri()); // check creds assertEquals("Blob creds don't match account", account.getCredentials(), blob.getCredentials()); assertEquals("Queue creds don't match account", account.getCredentials(), queue.getCredentials()); assertEquals("Table creds don't match account", account.getCredentials(), table.getCredentials()); assertEquals("File creds don't match account", account.getCredentials(), file.getCredentials()); }
Utility.assertNotNull("shareName", shareName); this.storageUri = PathUtility.appendPathToUri(client.getStorageUri(), shareName); this.name = shareName; this.snapshotID = snapshotID;
/** * Returns a result segment of an enumerable collection of shares for this File service client. * * @return A {@link ResultSegment} object that contains a segment of the enumerable collection of * {@link CloudFileShare} objects that represent the shares for this client. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public ResultSegment<CloudFileShare> listSharesSegmented() throws StorageException { return this.listSharesSegmented(null, EnumSet.noneOf(ShareListingDetails.class), null, null /* continuationToken */, null /* options */, null /* opContext */); }
/** * Uploads a new {@link FileServiceProperties} configuration to the given storage service. This encapsulates * the CORS configurations. * * @param properties * A {@link FileServiceProperties} object which specifies the service properties to upload. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public void uploadServiceProperties(final FileServiceProperties properties) throws StorageException { this.uploadServiceProperties(properties, null /* options */, null /* opContext */); }
/** * Uses the concurrent request count from the specified client if <code>null</code>, sets a default value for * everything else, and sets defaults as defined in the parent class. * * @param options * The input options to copy from when applying defaults * @param client * A {@link CloudFileClient} object that represents the service client used to set the default timeout * interval and retry policy, if they are <code>null</code>. Additionally, the default value of * {@link #concurrentRequestCount} is <code>1</code>. * @param setStartTime * whether to initialize the startTimeInMs field, or not */ protected static final FileRequestOptions populateAndApplyDefaults(final FileRequestOptions options, final CloudFileClient client, final boolean setStartTime) { FileRequestOptions modifiedOptions = new FileRequestOptions(options); FileRequestOptions.populateRequestOptions(modifiedOptions, client.getDefaultRequestOptions(), setStartTime); FileRequestOptions.applyDefaults(modifiedOptions); return modifiedOptions; }
/** * Retrieves the current {@link FileServiceProperties} for the given storage service. This encapsulates * the CORS configurations. * * @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 A {@link FileServiceProperties} object representing the current configuration of the service. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public final FileServiceProperties downloadServiceProperties(FileRequestOptions options, OperationContext opContext) throws StorageException { if (opContext == null) { opContext = new OperationContext(); } opContext.initialize(); options = FileRequestOptions.populateAndApplyDefaults(options, this); return new FileServiceProperties(ExecutionEngine.executeWithRetry( this, null, this.downloadServicePropertiesImpl(options, false), options.getRetryPolicyFactory(), opContext)); }
readListShare.getServiceClient().getCredentials().getClass().toString()); readListShare.getStorageUri(), this.share.generateSharedAccessSignature(null, "readlist"))); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), shareFromUri.getServiceClient().getCredentials().getClass().toString()); CloudFileClient client = new CloudFileClient(this.share.getServiceClient().getStorageUri(), creds); CloudFileShare shareFromClient = client.getShareReference(readListShare.getName()); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), shareFromClient.getServiceClient().getCredentials().getClass().toString()); assertEquals(client, shareFromClient.getServiceClient());