ShareServiceClient Class
- java.
lang. Object - com.
azure. storage. file. share. ShareServiceClient
- com.
public final class ShareServiceClient
This class provides a shareServiceAsyncClient that contains all the operations for interacting with a file account in Azure Storage. Operations allowed by the shareServiceAsyncClient are creating, listing, and deleting shares and retrieving and updating properties of the account.
Instantiating a Synchronous File Service Client
ShareServiceClient client = new ShareServiceClientBuilder()
.connectionString("${connectionString}")
.endpoint("${endpoint}")
.buildClient();
View ShareServiceClientBuilder for additional ways to construct the shareServiceAsyncClient.
Method Summary
Methods inherited from java.lang.Object
Method Details
createShare
public ShareClient createShare(String shareName)
Creates a share in the storage account with the specified name and returns a ShareClient to interact with it.
Code Samples
Create the share with share name of "myshare"
fileServiceClient.createShare("myshare");
System.out.println("Creating the share completed.");
For more information, see the Azure Docs.
Parameters:
Returns:
createShareWithResponse
public Response<ShareClient> createShareWithResponse(String shareName, Map<String,String> metadata, Integer quotaInGB, Duration timeout, Context context)
Creates a share in the storage account with the specified name and metadata and returns a ShareClient to interact with it.
Code Samples
Create the share "test" with a quota of 10 GB
Response<ShareClient> response = fileServiceClient.createShareWithResponse("test",
Collections.singletonMap("share", "metadata"), null, Duration.ofSeconds(5),
new Context(key1, value1));
System.out.printf("Creating the share completed with status code %d", response.getStatusCode());
For more information, see the Azure Docs.
For more information on updated max file share size values, see the Azure Docs.
Parameters:
Returns:
createShareWithResponse
public Response<ShareClient> createShareWithResponse(String shareName, ShareCreateOptions options, Duration timeout, Context context)
Creates a share in the storage account with the specified name and options and returns a ShareClient to interact with it.
Code Samples
Response<ShareClient> response = fileServiceClient.createShareWithResponse("test",
new ShareCreateOptions().setMetadata(Collections.singletonMap("share", "metadata")).setQuotaInGb(1)
.setAccessTier(ShareAccessTier.HOT), Duration.ofSeconds(5), new Context(key1, value1));
System.out.printf("Creating the share completed with status code %d", response.getStatusCode());
For more information, see the Azure Docs.
Parameters:
Returns:
deleteShare
public void deleteShare(String shareName)
Deletes the share in the storage account with the given name
Code Samples
Delete the share "test"
fileServiceClient.deleteShare("myshare");
For more information, see the Azure Docs.
Parameters:
deleteShareWithResponse
public Response<Void> deleteShareWithResponse(String shareName, String snapshot, Duration timeout, Context context)
Deletes the specific snapshot of the share in the storage account with the given name. Snapshot are identified by the time they were created.
Code Samples
Delete the snapshot of share "test" that was created at current time.
OffsetDateTime midnight = OffsetDateTime.of(LocalDateTime.now(), ZoneOffset.UTC);
Response<Void> response = fileServiceClient.deleteShareWithResponse("test", midnight.toString(),
Duration.ofSeconds(1), new Context(key1, value1));
System.out.printf("Deleting the snapshot completed with status code %d", response.getStatusCode());
For more information, see the Azure Docs.
Parameters:
Returns:
generateAccountSas
public String generateAccountSas(AccountSasSignatureValues accountSasSignatureValues)
Generates an account SAS for the Azure Storage account using the specified AccountSasSignatureValues.
Note : The client must be authenticated via StorageSharedKeyCredential
See AccountSasSignatureValues for more information on how to construct an account SAS.
Generating an account SAS
The snippet below generates an AccountSasSignatureValues object that lasts for two days and gives the user read and list access to blob and file shares.
AccountSasPermission permissions = new AccountSasPermission()
.setListPermission(true)
.setReadPermission(true);
AccountSasResourceType resourceTypes = new AccountSasResourceType().setContainer(true);
AccountSasService services = new AccountSasService().setBlobAccess(true).setFileAccess(true);
OffsetDateTime expiryTime = OffsetDateTime.now().plus(Duration.ofDays(2));
AccountSasSignatureValues sasValues =
new AccountSasSignatureValues(expiryTime, permissions, services, resourceTypes);
// Client must be authenticated via StorageSharedKeyCredential
String sas = fileServiceClient.generateAccountSas(sasValues);
Parameters:
Returns:
String representing the SAS query parameters.generateAccountSas
public String generateAccountSas(AccountSasSignatureValues accountSasSignatureValues, Consumer<String> stringToSignHandler, Context context)
Generates an account SAS for the Azure Storage account using the specified AccountSasSignatureValues.
Note : The client must be authenticated via StorageSharedKeyCredential
See AccountSasSignatureValues for more information on how to construct an account SAS.
Parameters:
Returns:
String representing the SAS query parameters.generateAccountSas
public String generateAccountSas(AccountSasSignatureValues accountSasSignatureValues, Context context)
Generates an account SAS for the Azure Storage account using the specified AccountSasSignatureValues.
Note : The client must be authenticated via StorageSharedKeyCredential
See AccountSasSignatureValues for more information on how to construct an account SAS.
The snippet below generates a SAS that lasts for two days and gives the user read and list access to blob containers and file shares.
AccountSasPermission permissions = new AccountSasPermission()
.setListPermission(true)
.setReadPermission(true);
AccountSasResourceType resourceTypes = new AccountSasResourceType().setContainer(true);
AccountSasService services = new AccountSasService().setBlobAccess(true).setFileAccess(true);
OffsetDateTime expiryTime = OffsetDateTime.now().plus(Duration.ofDays(2));
AccountSasSignatureValues sasValues =
new AccountSasSignatureValues(expiryTime, permissions, services, resourceTypes);
// Client must be authenticated via StorageSharedKeyCredential
String sas = fileServiceClient.generateAccountSas(sasValues, new Context("key", "value"));
Parameters:
Returns:
String representing the SAS query parameters.getAccountName
public String getAccountName()
Get associated account name.
Returns:
getFileServiceUrl
public String getFileServiceUrl()
Get the url of the storage file service client.
Returns:
getHttpPipeline
public HttpPipeline getHttpPipeline()
Gets the HttpPipeline powering this client.
Returns:
getProperties
public ShareServiceProperties getProperties()
Retrieves the properties of the storage account's File service. The properties range from storage analytics and metrics to CORS (Cross-Origin Resource Sharing).
Code Samples
Retrieve File service properties
ShareServiceProperties properties = fileServiceClient.getProperties();
System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b", properties.getHourMetrics().isEnabled(),
properties.getMinuteMetrics().isEnabled());
For more information, see the Azure Docs.
Returns:
getPropertiesWithResponse
public Response<ShareServiceProperties> getPropertiesWithResponse(Duration timeout, Context context)
Retrieves the properties of the storage account's File service. The properties range from storage analytics and metrics to CORS (Cross-Origin Resource Sharing).
Code Samples
Retrieve File service properties
ShareServiceProperties properties = fileServiceClient.getPropertiesWithResponse(
Duration.ofSeconds(1), new Context(key1, value1)).getValue();
System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b", properties.getHourMetrics().isEnabled(),
properties.getMinuteMetrics().isEnabled());
For more information, see the Azure Docs.
Parameters:
Returns:
getServiceVersion
public ShareServiceVersion getServiceVersion()
Gets the service version the client is using.
Returns:
getShareClient
public ShareClient getShareClient(String shareName)
Constructs a ShareClient that interacts with the specified share.
If the share doesn't exist in the storage account create() in the shareServiceAsyncClient will need to be called before interaction with the share can happen.
Parameters:
Returns:
getUserDelegationKey
public UserDelegationKey getUserDelegationKey(OffsetDateTime start, OffsetDateTime expiry)
Gets a user delegation key for use with this account's share. Note: This method call is only valid when using TokenCredential in this object's HttpPipeline.
Parameters:
Returns:
getUserDelegationKeyWithResponse
public Response<UserDelegationKey> getUserDelegationKeyWithResponse(OffsetDateTime start, OffsetDateTime expiry, Duration timeout, Context context)
Gets a user delegation key for use with this account's share. Note: This method call is only valid when using TokenCredential in this object's HttpPipeline.
Parameters:
Returns:
listShares
public PagedIterable<ShareItem> listShares()
Lists all shares in the storage account without their metadata or snapshots.
Code Samples
List all shares in the account
fileServiceClient.listShares().forEach(
shareItem -> System.out.printf("Share %s exists in the account", shareItem.getName())
);
For more information, see the Azure Docs.
Returns:
listShares
public PagedIterable<ShareItem> listShares(ListSharesOptions options, Duration timeout, Context context)
Lists the shares in the Storage account that pass the options filter.
Set starts with name filter using setPrefix(String prefix) to filter shares that are listed.
Pass true to setIncludeMetadata(boolean includeMetadata) to have metadata returned for the shares.
Pass true to setIncludeSnapshots(boolean includeSnapshots) to have snapshots of the shares listed.
Code Samples
List all shares that begin with "azure"
fileServiceClient.listShares(new ListSharesOptions().setPrefix("azure"), Duration.ofSeconds(1),
new Context(key1, value1)).forEach(
shareItem -> System.out.printf("Share %s exists in the account", shareItem.getName())
);
List all shares including their snapshots and metadata
fileServiceClient.listShares(new ListSharesOptions().setIncludeMetadata(true)
.setIncludeSnapshots(true), Duration.ofSeconds(1), new Context(key1, value1)).forEach(
shareItem -> System.out.printf("Share %s exists in the account", shareItem.getName())
);
For more information, see the Azure Docs.
Parameters:
Returns:
setProperties
public void setProperties(ShareServiceProperties properties)
Sets the properties for the storage account's File service. The properties range from storage analytics and metric to CORS (Cross-Origin Resource Sharing).
To maintain the CORS in the Share service pass a null value for getCors(). To disable all CORS in the Share service pass an empty list for getCors().
Code Sample
Clear CORS in the File service
ShareServiceProperties properties = fileServiceClient.getProperties();
properties.setCors(Collections.emptyList());
Response<Void> response = fileServiceClient.setPropertiesWithResponse(properties,
Duration.ofSeconds(1), new Context(key1, value1));
System.out.printf("Setting File service properties completed with status code %d", response.getStatusCode());
Enable Minute and Hour Metrics
ShareServiceProperties properties = fileServiceClient.getProperties();
properties.getMinuteMetrics().setEnabled(true);
properties.getHourMetrics().setEnabled(true);
fileServiceClient.setProperties(properties);
System.out.println("Setting File service properties completed.");
For more information, see the Azure Docs.
Parameters:
setPropertiesWithResponse
public Response<Void> setPropertiesWithResponse(ShareServiceProperties properties, Duration timeout, Context context)
Sets the properties for the storage account's File service. The properties range from storage analytics and metric to CORS (Cross-Origin Resource Sharing). To maintain the CORS in the Share service pass a null value for getCors(). To disable all CORS in the Share service pass an empty list for getCors().
Code Sample
Clear CORS in the File service
ShareServiceProperties properties = fileServiceClient.getProperties();
properties.setCors(Collections.emptyList());
Response<Void> response = fileServiceClient.setPropertiesWithResponse(properties,
Duration.ofSeconds(1), new Context(key1, value1));
System.out.printf("Setting File service properties completed with status code %d", response.getStatusCode());
Enable Minute and Hour Metrics
ShareServiceProperties properties = fileServiceClient.getPropertiesWithResponse(
Duration.ofSeconds(1), new Context(key1, value1)).getValue();
properties.getMinuteMetrics().setEnabled(true);
properties.getHourMetrics().setEnabled(true);
Response<Void> response = fileServiceClient.setPropertiesWithResponse(properties,
Duration.ofSeconds(1), new Context(key1, value1));
System.out.printf("Setting File service properties completed with status code %d", response.getStatusCode());
For more information, see the Azure Docs.
Parameters:
Returns:
undeleteShare
public ShareClient undeleteShare(String deletedShareName, String deletedShareVersion)
Restores a previously deleted share.
If the share associated with provided deletedShareName already exists, this call will result in a 409 (conflict).
This API is only functional if Share Soft Delete is enabled for the storage account associated with the share. For more information, see the Azure Docs.
Code Samples
ListSharesOptions listSharesOptions = new ListSharesOptions();
listSharesOptions.setIncludeDeleted(true);
fileServiceClient.listShares(listSharesOptions, Duration.ofSeconds(1), context).forEach(
deletedShare -> {
ShareClient shareClient = fileServiceClient.undeleteShare(
deletedShare.getName(), deletedShare.getVersion());
}
);
For more information, see the Azure Docs.
Parameters:
Returns:
undeleteShareWithResponse
public Response<ShareClient> undeleteShareWithResponse(String deletedShareName, String deletedShareVersion, Duration timeout, Context context)
Restores a previously deleted share.
If the share associated with provided deletedShareName already exists, this call will result in a 409 (conflict).
This API is only functional if Share Soft Delete is enabled for the storage account associated with the share. For more information, see the Azure Docs.
Code Samples
ListSharesOptions listSharesOptions = new ListSharesOptions();
listSharesOptions.setIncludeDeleted(true);
fileServiceClient.listShares(listSharesOptions, Duration.ofSeconds(1), context).forEach(
deletedShare -> {
ShareClient shareClient = fileServiceClient.undeleteShareWithResponse(
deletedShare.getName(), deletedShare.getVersion(), Duration.ofSeconds(1), context).getValue();
}
);
For more information, see the Azure Docs.
Parameters:
Returns: