Over a million developers have joined DZone.

Programming Azure Blobs With Java

Check out how to use the Azure SDK with Java to store your content in the Cloud.

· Cloud Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! Brought to you in partnership with MongoDB.

I’m producing a series of blog posts that highlights the work I’ve done with an O’Reilly media video course. I will produce several posts a week to support this course, as seen below.

http://bit.ly/bruno-does-linux-data-java

smallslide0

The course is targeted towards developers who want to write Java applications on Linux and host those applications in Azure.

These applications talk to today’s top data stores, including:

  • Azure Tables
  • Azure Blobs
  • Azure Queues
  • SQL Server
  • SQL Database
  • MySQL
  • PostGres
  • DocumentDB
  • MongoDB
  • Cassandra
  • Redis

Using Java to Connect to Azure Blobs

  • Reading and writing blobs is easy with the Azure SDK
  • You will need to:
    • Provision a storage account
    • Reference a container
    • Write code for the blob operations (upload, delete, download, etc)

smallslide001

Slide 1 – Overview

You Can Find the Java Storage SDK

  • You can compile your own jar from the latest sources

smallslide002

Slide 2 – Finding the SDK

The Github Repo With the Java SDK

  • Source code is available
  • We will compile our own Jar file

smallslide003

Slide 3 – Github

Git Clone

  • Downloading the Azure Java Storage SDK

smallslide004

Slide 4 – Downloading the SDK to local dev computer

Maven Compile

  • You will need to install and run Maven
  • Explained in course at top of this post

smallslide014

Slide 5 – Compiling the .java to .class

Using the Jar Utility to Create a JAR File

  • Creating a jar file from all the class files
  • Review
    • Download sources
    • Compile sources
    • Create jar file
    • Add jar file to Eclipse project (future module)

smallslide005

Slide 6 – Creating jar file

Viewing Jar File

  • You can now see the jar file created
  • You may want to copy it to an easier location
    • Like c:\azure-storage-java

smallslide007

Slide 7 – Viewing the jar file

Moved Jar File to New Location

  • Don’t want it too deeply buried

smallslide008

Slide 8 – New jar file location

Creating a New Java Project in Eclipse

  • Obviously, I assume you’ve installed Eclipse
  • Issue a File/New/Java Project

smallslide009

Slide 9 – Installing Eclipse

Filling Out the Connection String

  • We have added BlobManager.java
  • You need to go to the portal to fill out the rest
  • Notice the import statements for the Azure Java Storage SDK

smallslide010

Slide 10 – Adding BlobManager.java

The Azure Portal

  • Notice we have:
    • Storage Account Name
    • Primary Key
  • We will need to paste this into our code

smallslide011

Slide 11 – Azure Portal

Storage Connection String

  • Notice on line 11 and 12 we have storage account name and primary account key pasted
  • Next we will go to try {} block on line 16

smallslide012

Slide 12 – Finished connection string

Connecting to Storage Account

  • Using CloudStorageAccount and CloudBlobClient objects
  • Lines 16 and 17

smallslide013

Slide 13 – Code to connect

Make Sure You Have the azurestorage.jar Properly Referenced

  • It is part of project properties
  • After adding the azurestorage.jar file to project you need to make sure it is part of the Java Build Path.

smallslide015

Slide 14 – Java build Path

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:
azure ,cloud ,java

Published at DZone with permission of Bruno Terkaly, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}