Java Audi Connect Download Client

Posted on
-->

In this quickstart, you learn to manage blobs by using Java. Blobs are objects that can hold large amounts of text or binary data, including images, documents, streaming media, and archive data. You'll upload, download, and list blobs. You'll also create, set permissions on, and delete containers.

Note

I found that setting the time out settings in HttpConnectionParams and HttpConnectionManager did not solve our case. We're limited to using org.apache.commons.httpclient version 3.0.1.

This quickstart uses a legacy version of the Azure Blob storage client library. To get started with the latest version, see Quickstart: Manage blobs with Java v12 SDK.

Prerequisites

  • An Azure account with an active subscription. Create an account for free.
  • An Azure Storage account. Create a storage account.
  • An IDE that has Maven integration. This guide uses Eclipse with the 'Eclipse IDE for Java Developers' configuration.

Download the sample application

The sample application is a basic console application.

Use git to download a copy of the application to your development environment.

This command clones the repository to your local git folder. To open the project, launch Eclipse and close the Welcome screen. Select File then Open Projects from File System. Make sure Detect and configure project natures is checked. Select Directory then navigate to where you stored the cloned repository. Inside the cloned repository, select the blobAzureApp folder. Make sure the blobAzureApp project appears as an Eclipse project, then select Finish.

Once the project completes importing, open AzureApp.java (located in blobQuickstart.blobAzureApp inside of src/main/java), and replace the accountname and accountkey inside of the storageConnectionString string. Then run the application. Specific instructions for completing these tasks are described in the following sections.

Copy your credentials from the Azure portal

The sample application needs to authenticate access to your storage account. To authenticate, add your storage account credentials to the application as a connection string. View your storage account credentials by following these steps:

  1. Sign in to the Azure portal.

  2. Locate your storage account.

  3. In the Settings section of the storage account overview, select Access keys. Here, you can view your account access keys and the complete connection string for each key.

  4. Find the Connection string value under key1, and select the Copy button to copy the connection string. You will add the connection string value to an environment variable in the next step.

Configure your storage connection string

In the application, you must provide the connection string for your storage account. Open the AzureApp.Java file. Find the storageConnectionString variable and paste the connection string value that you copied in the previous section. Your storageConnectionString variable should look similar to the following code example:

Run the sample

This sample application creates a test file in your default directory (C:Users<user>AppDataLocalTemp, for Windows users), uploads it to Blob storage, lists the blobs in the container, then downloads the file with a new name so you can compare the old and new files.

Run the sample using Maven at the command line. Open a shell and navigate to blobAzureApp inside of your cloned directory. Then enter mvn compile exec:java.

The following example shows the output if you were to run the application on Windows.

Before you continue, check your default directory (C:Users<user>AppDataLocalTemp, for Windows users) for the sample file. Copy the URL for the blob out of the console window and paste it into a browser to view the contents of the file in Blob storage. If you compare the sample file in your directory with the contents stored in Blob storage, you will see that they are the same.

Note

You can also use a tool such as the Azure Storage Explorer to view the files in Blob storage. Azure Storage Explorer is a free cross-platform tool that allows you to access your storage account information.

ConnectAudi

After you've verified the files, press the Enter key to complete the demo and delete the test files. Now that you know what the sample does, open the AzureApp.java file to look at the code.

Understand the sample code

Next, we walk through the sample code so that you can understand how it works.

Get references to the storage objects

The first thing to do is create the references to the objects used to access and manage Blob storage. These objects build on each other -- each is used by the next one in the list.

  • Create an instance of the CloudStorageAccount object pointing to the storage account.

    The CloudStorageAccount object is a representation of your storage account and it allows you to set and access storage account properties programmatically. Using the CloudStorageAccount object you can create an instance of the CloudBlobClient, which is necessary to access the blob service.

  • Create an instance of the CloudBlobClient object, which points to the Blob service in your storage account.

    The CloudBlobClient provides you a point of access to the blob service, allowing you to set and access Blob storage properties programmatically. Using the CloudBlobClient you can create an instance of the CloudBlobContainer object, which is necessary to create containers.

  • Create an instance of the CloudBlobContainer object, which represents the container you are accessing. Use containers to organize your blobs like you use folders on your computer to organize your files.

    Once you have the CloudBlobContainer, you can create an instance of the CloudBlockBlob object that points to the specific blob you're interested in, and perform an upload, download, copy, or other operation.

Important

Container names must be lowercase. For more information about containers, see Naming and Referencing Containers, Blobs, and Metadata.

Create a container

In this section, you create an instance of the objects, create a new container, and then set permissions on the container so the blobs are public and can be accessed with just a URL. The container is called quickstartcontainer.

This example uses CreateIfNotExists because we want to create a new container each time the sample is run. In a production environment, where you use the same container throughout an application, it's better practice to only call CreateIfNotExists once. Alternatively, you can create the container ahead of time so you don't need to create it in the code.

Upload blobs to the container

To upload a file to a block blob, get a reference to the blob in the target container. Once you have the blob reference, you can upload data to it by using Cloud​Block​Blob.​Upload​. This operation creates the blob if it doesn't already exist, or overwrites the blob if it already exists.

The sample code creates a local file to be used for the upload and download, storing the file to be uploaded as source and the name of the blob in blob. The following example uploads the file to your container called quickstartcontainer.

There are several upload methods including upload, uploadBlock, uploadFullBlob, uploadStandardBlobTier, and uploadText which you can use with Blob storage. For example, if you have a string, you can use the UploadText method rather than the Upload method.

Block blobs can be any type of text or binary file. Page blobs are primarily used for the VHD files that back IaaS VMs. Use append blobs for logging, such as when you want to write to a file and then keep adding more information. Most objects stored in Blob storage are block blobs.

List the blobs in a container

You can get a list of files in the container using Cloud​Blob​Container.​List​Blobs. The following code retrieves the list of blobs, then loops through them, showing the URIs of the blobs found. You can copy the URI from the command window and paste it into a browser to view the file.

Download blobs

Java Audi Connect Download Client Windows 7

Download blobs to your local disk using Cloud​Blob.​Download​To​File.

Java Audi Connect Download Client Login

The following code downloads the blob uploaded in a previous section, adding a suffix of '_DOWNLOADED' to the blob name so you can see both files on local disk.

Clean up resources

If you no longer need the blobs that you have uploaded, you can delete the entire container using Cloud​Blob​Container.​DeleteIfExists. This method also deletes the files in the container.

Java Audi Connect Download Client Download

Next steps

Java Audi Connect Download Client Windows 10

In this article, you learned how to transfer files between a local disk and Azure Blob storage using Java. To learn more about working with Java, continue to our GitHub source code repository.