This project provides a client library in Java that makes it easy to consume Microsoft Azure Storage services. For documentation, please see the Microsoft Azure Java Developer Center and the Java client library reference for Azure Storage.
This README page is a reference to the SDK v7. For the new asynchronous SDK v10, go to Storage SDK v10 for Java (Preview).
SDK Name | Version | Description | Maven/API Reference Links |
---|---|---|---|
Storage SDK v10 for Java | v10-Preview | The next generation async Storage SDK (Blob only) | Maven - Reference |
Storage SDK v7 for Java | v7 | Legacy Storage SDK (sync only) | Maven - Reference |
Storage SDK for Android | v2 | Storage SDK for Android | Maven - Reference |
Azure Management Libraries for Java | v1 | Management libraries including Storage Resource Provider APIs | Maven - Reference |
- Blob
- Create/Read/Update/Delete containers
- Create/Read/Update/Delete blobs
- Advanced Blob Operations
- Queue
- Create/Delete Queues
- Insert/Peek Queue Messages
- Advanced Queue Operations
- Table
- Create/Read/Update/Delete tables
- Create/Read/Update/Delete entities
- Batch operations
- Advanced Table Operations
To get the binaries of this library as distributed by Microsoft, ready for use within your project, you can use Maven.
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>8.0.0</version>
</dependency>
To get the source code of the SDK via git just type:
git clone git://github.com/Azure/azure-storage-java.git
cd ./azure-storage-java
mvn compile
To download a copy of the source code, click "Download ZIP" on the right side of the page or click here. Unzip and navigate to the microsoft-azure-storage folder.
- Java 1.7+
- Jackson-Core is used for JSON parsing.
- (Optional) SLF4J is a logging facade.
- (Optional) SLF4J binding is used to associate a specific logging framework with SLF4J.
- (Optional) Maven
The two dependencies, Jackson-Core and SLF4J, will be added automatically if Maven is used. Otherwise, please download the jars and add them to your build path.
SLF4J is only needed if you enable logging through the OperationContext class. If you plan to use logging, please also download an SLF4J binding which will link the SLF4J API with the logging implementation of your choice. Simple is a good default. See the SLF4J user manual for more information.
To use this SDK to call Microsoft Azure storage services, you need to first create an account.
Samples are provided in the microsoft-azure-storage-samples folder. The unit tests in microsoft-azure-storage-test can also be helpful.
The following is a quick example on how to upload a file to azure blob and download it back. You may also download and view the samples in the microsoft-azure-storage-samples folder. For additional information on using the client libraries to access Azure services see the How To guides for blobs, queues, tables and the general documentation.
import java.io.*;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
public class BlobSample {
public static final String storageConnectionString =
"DefaultEndpointsProtocol=http;"
+ "AccountName=your_account_name;"
+ "AccountKey= your_account_key";
public static void main(String[] args) {
try {
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient serviceClient = account.createCloudBlobClient();
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("myimages");
container.createIfNotExists();
// Upload an image file.
CloudBlockBlob blob = container.getBlockBlobReference("image1.jpg");
File sourceFile = new File("c:\\myimages\\image1.jpg");
blob.upload(new FileInputStream(sourceFile), sourceFile.length());
// Download the image file.
File destinationFile = new File(sourceFile.getParentFile(), "image1Download.tmp");
blob.downloadToFile(destinationFile.getAbsolutePath());
}
catch (FileNotFoundException fileNotFoundException) {
System.out.print("FileNotFoundException encountered: ");
System.out.println(fileNotFoundException.getMessage());
System.exit(-1);
}
catch (StorageException storageException) {
System.out.print("StorageException encountered: ");
System.out.println(storageException.getMessage());
System.exit(-1);
}
catch (Exception e) {
System.out.print("Exception encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
}
}
}
Be sure to check out the Microsoft Azure Developer Forums on MSDN or the Developer Forums on Stack Overflow if you have trouble with the provided code.
If you would like to become an active contributor to this project please follow the instructions provided in Azure Projects Contribution Guidelines.
If you encounter any bugs with the library please file an issue in the Issues section of the project.
When sending pull requests, please send non-breaking PRs to the dev branch and breaking changes to the dev_breaking branch. Do not make PRs against master.