The Server Message Cake (SMB) protocol is the preferred file share protocol used on-premises today. The Microsoft Azure File Share service enables customers to leverage the availability and scalability of Azure'due south Deject Infrastructure every bit a Service (IaaS) SMB without having to rewrite SMB customer applications.

Files stored in Azure File Share service shares are attainable via the SMB protocol, and as well via REST APIs. The File Share service offers the following iv resources: the storage account, shares, directories, and files. Shares provide a manner to organize sets of files and also can be mounted as an SMB file share that is hosted in the deject.

Source lawmaking | API reference documentation | REST API documentation | Production documentation | Samples

Getting started

Prerequisites

  • Java Evolution Kit (JDK) with version 8 or above
  • Azure Subscription
  • Create Storage Account

Include the package

Include the BOM file

Please include the azure-sdk-bom to your project to accept dependency on GA version of the library. In the following snippet, supersede the {bom_version_to_target} placeholder with the version number. To larn more most the BOM, see the AZURE SDK BOM README.

              <dependencyManagement>     <dependencies>         <dependency>             <groupId>com.azure</groupId>             <artifactId>azure-sdk-bom</artifactId>             <version>{bom_version_to_target}</version>             <type>pom</type>             <telescopic>import</scope>         </dependency>     </dependencies> </dependencyManagement>                          

and then include the direct dependency in the dependencies department without the version tag.

              <dependencies>   <dependency>     <groupId>com.azure</groupId>     <artifactId>azure-storage-file-share</artifactId>   </dependency> </dependencies>                          

Include directly dependency

If y'all want to have dependency on a item version of the library that is not nowadays in the BOM, add the straight dependency to your projection as follows.

              <dependency>   <groupId>com.azure</groupId>   <artifactId>azure-storage-file-share</artifactId>   <version>12.12.0</version> </dependency>                          

Create a Storage Account

To create a Storage Account you tin can use the Azure Portal or Azure CLI.

              az storage account create \     --resource-group <resource-group-name> \     --name <storage-account-proper noun> \     --location <location>                          

Authenticate the client

In order to interact with the Storage service (File Share Service, Share, Directory, MessageId, File) you'll need to create an example of the Service Client class. To brand this possible you'll need the Account SAS (shared access signature) string of Storage account. Acquire more at SAS Token

Get Credentials

  • SAS Token

    • Use the Azure CLI snippet below to become the SAS token from the Storage account.

                            az storage file generate-sas     --name {business relationship proper name}     --expiry {date/time to expire SAS token}     --permission {permission to grant}     --connexion-string {connexion string of the storage account}                                          
                            CONNECTION_STRING=<connexion-cord>  az storage file generate-sas     --name javasdksas     --expiry 2019-06-05     --permission rpau     --connection-string $CONNECTION_STRING                                          
    • Alternatively, get the Account SAS Token from the Azure Portal.

      1. Go to your storage business relationship.
      2. Click on "Shared admission signature".
      3. Click on "Generate SAS and connexion string".
  • Shared Key Credential

    • In that location are two ways to create a shared key credential, the first is using the storage business relationship name and business relationship key. The second is using the storage connection string.
      1. Utilize account name and business relationship cardinal.
        1. The account name is your storage business relationship name.
        2. Go to your storage business relationship.
        3. Select "Admission keys" tab.
        4. Re-create the "Cardinal" value for either Central i or Key 2.
      2. Use the connexion string
        1. Become to your storage business relationship.
        2. Select "Access keys" tab.
        3. Copy the "Connection string" value for either Cardinal 1 or Primal 2.

Fundamental concepts

URL format

File Shares are addressable using the following URL format:

              https://<storage account>.file.cadre.windows.cyberspace/<share>                          

The following URL addresses a queue in the diagram:

              https://myaccount.file.cadre.windows.cyberspace/images-to-download                          

Resource URI Syntax

For the storage business relationship, the base URI for queue operations includes the name of the account only:

              https://myaccount.file.cadre.windows.net                          

For file, the base URI includes the name of the business relationship and the proper name of the directory/file:

              https://myaccount.file.cadre.windows.internet/myshare/mydirectorypath/myfile                          

Handling Exceptions

Uses the shareServiceClient generated from shareServiceClient section below.

              try {     shareServiceClient.createShare("myShare"); } catch (ShareStorageException e) {     logger.error("Failed to create a share with error code: " + e.getErrorCode()); }                          

Resource Names

The URI to reference a share, directory or file must be unique. Inside a given storage account, every share must accept a unique name. Every file within a given share or directory must also have a unique proper noun within that share or directory.

If you attempt to create a share, directory, or file with a proper noun that violates naming rules, the request will fail with condition code 400 (Bad Request).

The rules for File Share service names are more restrictive than what is prescribed by the SMB protocol for SMB share names, so that the Hulk and File services tin share similar naming conventions for containers and shares. The naming restrictions for shares are every bit follows:

  1. A share name must be a valid DNS proper noun.
  2. Share names must commencement with a letter or number, and can contain just letters, numbers, and the dash (-) character.
  3. Every dash (-) character must be immediately preceded and followed by a letter or number; sequent dashes are not permitted in share names.
  4. All letters in a share name must be lowercase.
  5. Share names must exist from 3 through 63 characters long.

Directory and File Names

The Azure File Share service naming rules for directory and file names are every bit follows:

  1. Share Directory and file names are case-preserving and case-insensitive.
  2. Share Directory and file component names must be no more than 255 characters in length.
  3. Share Directory names cannot end with the frontward slash character (/). If provided, information technology will be automatically removed.
  4. Share File names must not finish with the frontward slash character (/).
  5. Reserved URL characters must be properly escaped.
  6. The following characters are not allowed: " \ / : | < > * ?
  7. Illegal URL path characters not allowed. Lawmaking points like \uE000, while valid in NTFS filenames, are not valid Unicode characters. In add-on, some ASCII or Unicode characters, like control characters (0x00 to 0x1F, \u0081, etc.), are too non allowed. For rules governing Unicode strings in HTTP/i.1 see RFC 2616, Department 2.2: Basic Rules and RFC 3987.
  8. The following file names are non allowed: LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, PRN, AUX, NUL, CON, CLOCK$, dot character (.), and two dot characters (..).

Metadata Names

Metadata for a share or file resource is stored as name-value pairs associated with the resource. Directories do not have metadata. Metadata names must attach to the naming rules for C# identifiers.

Note that metadata names preserve the instance with which they were created, but are instance-insensitive when set or read. If two or more metadata headers with the same name are submitted for a resources, the Azure File service returns status code 400 (Bad Request).

The File Share Service Residual API provides operations on accounts and manage file service properties. It allows the operations of listing and deleting shares, getting and setting file service backdrop. Once y'all have the SASToken, you can construct the shareServiceClient with ${accountName}, ${sasToken}

              String shareServiceURL = String.format("https://%south.file.core.windows.net", ACCOUNT_NAME); ShareServiceClient shareServiceClient = new ShareServiceClientBuilder().endpoint(shareServiceURL)     .sasToken(SAS_TOKEN).buildClient();                          

The share resource includes metadata and properties for that share. Information technology allows the operations of creating, creating snapshot, deleting shares, getting share properties, setting metadata, getting and setting ACL (Access policy). Getting and setting ACL (Access policy) can but be used by ShareClient with ConnectionString.

One time you have the SASToken, you can construct the share client with ${accountName}, ${shareName}, ${sasToken}

              String shareURL = String.format("https://%s.file.core.windows.cyberspace", ACCOUNT_NAME); ShareClient shareClient = new ShareClientBuilder().endpoint(shareURL)     .sasToken(SAS_TOKEN).shareName(shareName).buildClient();                          

Once you lot have the ConnectionString, you can construct the share client with ${accountName}, ${shareName}, ${connectionString}

              Cord shareURL = String.format("https://%s.file.cadre.windows.cyberspace", ACCOUNT_NAME); ShareClient shareClient = new ShareClientBuilder().endpoint(shareURL)     .connectionString(CONNECTION_STRING).shareName(shareName).buildClient();                          

Directory

The directory resource includes the properties for that directory. It allows the operations of creating, listing, deleting directories or subdirectories or files, getting properties, setting metadata, listing and force closing the handles. Once yous take the SASToken, you can construct the file service customer with ${accountName}, ${shareName}, ${directoryPath}, ${sasToken}

              Cord directoryURL = String.format("https://%southward.file.core.windows.cyberspace", ACCOUNT_NAME); ShareDirectoryClient directoryClient = new ShareFileClientBuilder().endpoint(directoryURL)     .sasToken(SAS_TOKEN).shareName(shareName).resourcePath(directoryPath).buildDirectoryClient();                          

File

The file resources includes the properties for that file. It allows the operations of creating, uploading, copying, downloading, deleting files or range of the files, getting properties, setting metadata, listing and forcefulness closing the handles. One time yous have the SASToken, y'all can construct the file service client with ${accountName}, ${shareName}, ${directoryPath}, ${fileName}, ${sasToken}

              String fileURL = String.format("https://%s.file.cadre.windows.net", ACCOUNT_NAME); ShareFileClient fileClient = new ShareFileClientBuilder().connectionString(CONNECTION_STRING)     .endpoint(fileURL).shareName(shareName).resourcePath(directoryPath + "/" + fileName).buildFileClient();                          

Examples

The following sections provide several lawmaking snippets covering some of the most common Configuration Service tasks, including:

  • Create a Share
  • Create a snapshot on Share
  • Create a Directory
  • Create a Subdirectory
  • Create a File
  • Listing all Shares
  • List all Subdirectories and Files
  • Listing all ranges on file
  • Delete a Share
  • Delete a Directory
  • Delete a Subdirectory
  • Delete a File
  • Copy a File
  • Arrest copy a File
  • Upload information to Storage File
  • Upload information bigger than iv MB to Storage File
  • Upload file to Storage File
  • Download data from file range
  • Download file from Storage File
  • Go a share service backdrop
  • Prepare a share service properties
  • Prepare a Share metadata
  • Become a Share access policy
  • Set a Share access policy
  • Go handles on Directory and File
  • Force close handles on handle id
  • Prepare quota on Share
  • Prepare file httpHeaders

Create a share in the Storage Business relationship. Throws StorageException If the share fails to be created. Taking a ShareServiceClient in KeyConcept, ${shareServiceClient}.

              String shareName = "testshare"; shareServiceClient.createShare(shareName);                          

Taking a ShareServiceClient in KeyConcept, ${shareServiceClient}.

              Cord shareName = "testshare"; ShareClient shareClient = shareServiceClient.getShareClient(shareName); shareClient.createSnapshot();                          

Create a directory

Taking the ${shareClient} initialized above, ${shareClient}.

              String dirName = "testdir"; shareClient.createDirectory(dirName);                          

Create a subdirectory

Taking the directoryClient in KeyConcept, ${directoryClient}.

              Cord subDirName = "testsubdir"; directoryClient.createSubdirectory(subDirName);                          

Create a File

Taking the directoryClient in KeyConcept, ${directoryClient} .

              Cord fileName = "testfile"; long maxSize = 1024; directoryClient.createFile(fileName, maxSize);                          

List all Shares

Taking the shareServiceClient in KeyConcept, ${shareServiceClient}

              shareServiceClient.listShares();                          

List all subdirectories and files

Taking the directoryClient in KeyConcept, ${directoryClient}

              directoryClient.listFilesAndDirectories();                          

List all ranges on file

Taking the fileClient in KeyConcept, ${fileClient}

              fileClient.listRanges();                          

Taking the shareClient in KeyConcept, ${shareClient}

              shareClient.delete();                          

Delete a directory

Taking the shareClient in KeyConcept, ${shareClient} .

              String dirName = "testdir"; shareClient.deleteDirectory(dirName);                          

Delete a subdirectory

Taking the directoryClient in KeyConcept, ${directoryClient} .

              String subDirName = "testsubdir"; directoryClient.deleteSubdirectory(subDirName);                          

Delete a file

Taking the directoryClient in KeyConcept, ${directoryClient} .

              Cord fileName = "testfile"; directoryClient.deleteFile(fileName);                          

Copy a file

Taking the fileClient in KeyConcept, ${fileClient} with string of source URL.

              String sourceURL = "https://myaccount.file.core.windows.internet/myshare/myfile"; Duration pollInterval = Duration.ofSeconds(2); SyncPoller<ShareFileCopyInfo, Void> poller = fileClient.beginCopy(sourceURL, null, pollInterval);                          

Abort copy a file

Taking the fileClient in KeyConcept, ${fileClient} with the copy info response returned above ${copyId}=[copyInfoResponse](#copy-a-file).

              fileClient.abortCopy("copyId");                          

Upload data to storage

Taking the fileClient in KeyConcept, ${fileClient} with data of "default" .

              String uploadText = "default"; InputStream data = new ByteArrayInputStream(uploadText.getBytes(StandardCharsets.UTF_8)); fileClient.upload(data, uploadText.length());                          

Upload data bigger than 4 MB to storage

Taking the fileClient in KeyConcept, ${fileClient} with data of "default" .

              byte[] data = "Hello, data sample!".getBytes(StandardCharsets.UTF_8);  long chunkSize = ShareFileAsyncClient.FILE_DEFAULT_BLOCK_SIZE; if (data.length > chunkSize) {     for (int outset = 0; offset < data.length; offset += chunkSize) {         try {             // the last clamper size is smaller than the others             chunkSize = Math.min(information.length - first, chunkSize);              // select the clamper in the byte array             byte[] subArray = Arrays.copyOfRange(data, offset, (int) (offset + chunkSize));              // upload the chunk             fileClient.uploadWithResponse(new ByteArrayInputStream(subArray), chunkSize, (long) commencement, aught, Context.NONE);         } catch (RuntimeException e) {             logger.fault("Failed to upload the file", e);             if (Boolean.TRUE.equals(fileClient.exists())) {                 fileClient.delete();             }             throw east;         }     } } else {     fileClient.upload(new ByteArrayInputStream(data), information.length); }                          

Upload file to storage

Taking the fileClient in KeyConcept, ${fileClient} .

              String filePath = "${myLocalFilePath}"; fileClient.uploadFromFile(filePath);                          

Download data from file range

Taking the fileClient in KeyConcept, ${fileClient} with the range from 1024 to 2048.

              ShareFileRange fileRange = new ShareFileRange(0L, 2048L); OutputStream stream = new ByteArrayOutputStream(); fileClient.downloadWithResponse(stream, fileRange, false, null, Context.NONE);                          

Download file from storage

Taking the fileClient in KeyConcept, ${fileClient} and download to the file of filePath.

              String filePath = "${myLocalFilePath}"; fileClient.downloadToFile(filePath);                          

Taking a ShareServiceClient in KeyConcept, ${shareServiceClient} .

              shareServiceClient.getProperties();                          

Taking a ShareServiceClient in KeyConcept, ${shareServiceClient} .

              ShareServiceProperties properties = shareServiceClient.getProperties();  backdrop.getMinuteMetrics().setEnabled(truthful).setIncludeApis(true); properties.getHourMetrics().setEnabled(true).setIncludeApis(true);  shareServiceClient.setProperties(properties);                          

Taking the shareClient in KeyConcept, ${shareClient} .

              Map<String, String> metadata = Collections.singletonMap("directory", "metadata"); shareClient.setMetadata(metadata);                          

Taking the shareClient in KeyConcept, ${shareClient} .

              shareClient.getAccessPolicy();                          

Taking the shareClient in KeyConcept, ${shareClient} .

              ShareAccessPolicy accessPolicy = new ShareAccessPolicy().setPermissions("r")     .setStartsOn(OffsetDateTime.now(ZoneOffset.UTC))     .setExpiresOn(OffsetDateTime.now(ZoneOffset.UTC).plusDays(10)); ShareSignedIdentifier permission = new ShareSignedIdentifier().setId("mypolicy").setAccessPolicy(accessPolicy); shareClient.setAccessPolicy(Collections.singletonList(permission));                          

Go handles on directory file

Taking the directoryClient in KeyConcept, ${directoryClient}

              PagedIterable<HandleItem> handleItems = directoryClient.listHandles(zip, true, Duration.ofSeconds(xxx), Context.NONE);                          

Force close handles on handle id

Taking the directoryClient in KeyConcept, ${directoryClient} and the handle id returned to a higher place ${handleId}=[handleItems](#get-handles-on-directory-file)

              PagedIterable<HandleItem> handleItems = directoryClient.listHandles(null, true, Elapsing.ofSeconds(30), Context.NONE); String handleId = handleItems.iterator().adjacent().getHandleId(); directoryClient.forceCloseHandleWithResponse(handleId, Duration.ofSeconds(xxx), Context.NONE);                          

Taking the shareClient in KeyConcept, ${shareClient} .

              int quotaOnGB = ane; shareClient.setPropertiesWithResponse(new ShareSetPropertiesOptions().setQuotaInGb(quotaOnGB), cypher, Context.NONE);                          

Set file httpheaders

Taking the fileClient in KeyConcept, ${fileClient} .

              ShareFileHttpHeaders httpHeaders = new ShareFileHttpHeaders().setContentType("text/manifestly"); fileClient.setProperties(1024, httpHeaders, null, zilch);                          

Troubleshooting

General

When y'all interact with file using this Java customer library, errors returned by the service correspond to the aforementioned HTTP status codes returned for REST API requests. For example, if you lot attempt to retrieve a share that doesn't be in your Storage Account, a 404 error is returned, indicating Non Found.

Default HTTP Client

All client libraries past default use the Netty HTTP client. Calculation the above dependency will automatically configure the customer library to use the Netty HTTP client. Configuring or changing the HTTP client is detailed in the HTTP clients wiki.

Default SSL library

All customer libraries, by default, use the Tomcat-native Boring SSL library to enable native-level operation for SSL operations. The Boring SSL library is an uber jar containing native libraries for Linux / macOS / Windows, and provides amend performance compared to the default SSL implementation within the JDK. For more than information, including how to reduce the dependency size, refer to the performance tuning section of the wiki.

Next steps

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you take the right to, and actually practice, grant us the rights to apply your contribution. For details, visit https://cla.microsoft.com.

When y'all submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (due east.g., label, comment). Only follow the instructions provided by the bot. Yous will only need to do this one time across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more data see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

For details on contributing to this repository, run into the contributing guide.

  1. Fork it
  2. Create your characteristic branch (git checkout -b my-new-characteristic)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the co-operative (git push origin my-new-feature)
  5. Create new Pull Request

Impressions