Skip to content

Instantly share code, notes, and snippets.

@helayoty
Created October 21, 2019 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helayoty/a5a91829a038ef078f9e784004609af8 to your computer and use it in GitHub Desktop.
Save helayoty/a5a91829a038ef078f9e784004609af8 to your computer and use it in GitHub Desktop.
Azure SDK (Java)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>az-java-sdk</groupId>
<artifactId>study</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-data-appconfiguration</artifactId>
<version>1.0.0-preview.5</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.0.0-preview.5</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-keyvault-certificates</artifactId>
<version>4.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-keyvault-keys</artifactId>
<version>4.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-keyvault-secrets</artifactId>
<version>4.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventhubs</artifactId>
<version>5.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventhubs-checkpointstore-blob</artifactId>
<version>1.0.0-preview.2</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob-batch</artifactId>
<version>12.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob-cryptography</artifactId>
<version>12.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-file</artifactId>
<version>12.0.0-preview.4</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-queue</artifactId>
<version>12.0.0-preview.4</version>
</dependency>
</dependencies>
</project>
package study;
import com.azure.storage.blob.*;
import com.azure.storage.blob.models.ParallelTransferOptions;
import com.azure.storage.blob.specialized.BlockBlobAsyncClient;
import com.azure.storage.blob.specialized.BlockBlobClient;
import com.azure.storage.common.credentials.SharedKeyCredential;
import reactor.core.publisher.Flux;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Random;
public class StorageStudy {
String accountName = "<StorageAccountName>";
String accountKey = "<Key>";
String endpoint = String.format(Locale.ROOT, "<StorageURL" , accountName);
//-----------------------------------------------------------------------------------------------------------------
// Task 1 � Create new storage account at azure portal. Create container "books" and create a new blob step1.txt, add "Hello World" message,
// also upload couple of images to this container using the portal interface.
// create additional containers cont1, cont2 and cont3
// Using Azure SDK print the list of all the blobs available under the container �books� within your account.
static void task1()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("books");
blobContainerClient.listBlobsFlat().forEach(
blobItem -> System.out.println("This is the blob name: " + blobItem.getName())
);
System.out.println("end of task1-----------------------------------------------");
}
//Copy the list of blobs you received here:
/*
This is the blob name: DuP91AFV4AADyFn.jpg
This is the blob name: jLa.png
This is the blob name: step1.txt
*/
//How long this task took to implement(min): _2 hours___________________________________
//How difficult ( very easy, easy, ok, hard, very hard)____very easy__________________
//How and what can we improve: - documentation (different preview versions) was the reason I took that time to finish
// - function name doesn't describe the functionality well
//-----------------------------------------------------------------------------------------------------------------
// Task 2 � print the names of all containers in your storage account
static void task2()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
blobServiceClient.listBlobContainers().forEach(containerItem -> {
System.out.println("Container name: " + containerItem.getName());
});
//add your code here
System.out.println("end of task2-------------------------------------------------");
}
//Copy the list of blobs you received here:
/*
Container name: books
Container name: cont1
Container name: cont2
Container name: cont3
*/
//How long this task took to implement(min): _5 min___________________________________
//How difficult ( very easy, easy, ok, hard, very hard)________very easy______________
//How and what can we improve_____________NA______________________________
//-----------------------------------------------------------------------------------------------------------------
// Task 3a � retrieve blob step1.txt from the container �books� and print it's content
static void task3a()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("books");
BlobClient blobClient = blobContainerClient.getBlobClient("step1.txt");
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
blobClient.download(outputStream);
System.out.println(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end of task3a-------------------------------------------------");
}
//Copy the blob step1.txt content here: "Hello World"
//How long this task took to implement(min): ______15 min______________________________
//How difficult ( very easy, easy, ok, hard, very hard)______easy________________
//How and what can we improve______________NA_____________________________
//-----------------------------------------------------------------------------------------------------------------
// Task 3b � print the size ( bytes ) of the blob step1.txt from the container �books�
static void task3b()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("books");
BlobClient blobClient = blobContainerClient.getBlobClient("step1.txt");
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
blobClient.download(outputStream);
byte[] byteArray = new byte[outputStream.size()];
byteArray = outputStream.toByteArray();
System.out.println("size of the blob in bytes :" + byteArray.length);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end of task3b-------------------------------------------------");
}
//Add the size of the step2.txt blob here: 13
//How long this task took to implement(min): 2 min
//How difficult ( very easy, easy, ok, hard, very hard) easy
//How and what can we improve NA
//-----------------------------------------------------------------------------------------------------------------
// Task 3c � retrieve last 2 bytes from blob step1.txt located in the container �books� and print them
static void task3c()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("books");
BlobClient blobClient = blobContainerClient.getBlobClient("step1.txt");
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
blobClient.download(outputStream);
int size = outputStream.size();
String bolbArray = new String(outputStream.toByteArray());
System.out.println("the blob last 2 bytes : " + bolbArray.substring(size-3,size-2) );
System.out.println(" & " + bolbArray.substring(size-2,size-1) );
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end of task3c-------------------------------------------------");
}
//Copy retrieved content here:_______the blob last 2 bytes : l & d_______________________________________
//How long this task took to implement(min): _______ 1 hour_____________________________
//How difficult ( very easy, easy, ok, hard, very hard)_____ok_________________
//How and what can we improve What is the need for this check?
// why don't we have a method to return and display the specific byte?
//-----------------------------------------------------------------------------------------------------------------
// Task 4 � create a new container �mybooks� using the SDK
static void task4()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mybooks");
blobContainerClient.create();
//add your code here
System.out.println("end of task4---------------------------------------------------");
}
//How long this task took to implement(min): ______2 minutes______________________________
//How difficult ( very easy, easy, ok, hard, very hard)_________easy_____________
//How and what can we improve__________the sample here https://github.com/Azure/azure-sdk-for-java/blob/99fee6293ddc45b4666dba4fdba16fd697157e75/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BasicExample.java line 62
// is has getBlobContainerClient taking a container name + time. why?_________________________________
//-----------------------------------------------------------------------------------------------------------------
// Task 5 � upload a file from your local disk to newbook.txt blob in your new container �mybooks�.
//Before you start, please comment out the task4 from the main function
static void task5() throws IOException {
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mybooks");
BlockBlobClient blobClient = blobContainerClient.getBlobClient("newbook.txt").getBlockBlobClient();
File sourceFile = new File("\\newbook.txt");
FileInputStream inputStream = new FileInputStream(sourceFile);
blobClient.upload(inputStream, sourceFile.length());
//add your code here
System.out.println("end of task5-------------------------------------------------------");
}
//How long this task took to implement(min): _________5 min___________________________
//How difficult ( very easy, easy, ok, hard, very hard)________ok______________
//How and what can we improve____________________NA_______________________
//-----------------------------------------------------------------------------------------------------------------
//Task 6 � The gutenberg project contains a large number of ebooks online
// Write code that stores one of the large books in a new blob named newbook.txt in the mybooks container
// This URL points to a sample book: "http://www.gutenberg.org/files/59466/59466-0.txt"
static void task6() throws MalformedURLException {
String containerName = "mybooks";
SharedKeyCredential credential = new SharedKeyCredential(accountName, accountKey);
BlobServiceAsyncClient blobServiceClient = new BlobServiceClientBuilder().endpoint(endpoint).credential(credential)
.buildAsyncClient();
BlobContainerAsyncClient blobContainerClient = blobServiceClient.getBlobContainerAsyncClient(containerName);
// uncomment it in case you need to create your container
//blobContainerClient.create().block();
URL url = new URL("http://www.gutenberg.org/files/59466/59466-0.txt");
BlobAsyncClient blobClient = blobContainerClient.getBlobAsyncClient("newbook.txt");
blobClient.startCopyFromURL(url).subscribe(response -> System.out.printf("Copy identifier: %s%n", response));
System.out.println(" end of task6------------------------------------------------");
}
//How long this task took to implement(min): _________1 hour___________________________
//How difficult ( very easy, easy, ok, hard, very hard)__________hard____________
//How and what can we improve__________________it wasn't clear if I need to use block async to upload or just copy URL_________________________
//-----------------------------------------------------------------------------------------------------------------
// Task 7 - delete blob "newbook.txt" from "mybooks" container
static void task7()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mybooks");
BlockBlobClient blobClient = blobContainerClient.getBlobClient("newbook.txt").getBlockBlobClient();
blobClient.delete();
System.out.println("end of task7---------------------------------------------------------");
}
//How long this task took to implement(min): _____________1 min_______________________
//How difficult ( very easy, easy, ok, hard, very hard)_______very easy_______________
//How and what can we improve______________________NA_____________________
//-----------------------------------------------------------------------------------------------------------------
// Task 8 - delete your container �mybooks�
static void task8()
{
BlobServiceClient blobServiceClient = getBlobServiceClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mybooks");
blobContainerClient.delete();
System.out.println("end of task8-------------------------------------------------------------");
}
//How long this task took to implement(min): _____________1 min_______________________
//How difficult ( very easy, easy, ok, hard, very hard)_______very easy_______________
//How and what can we improve______________________NA_____________________
private static BlobServiceClient getBlobServiceClient(){
SharedKeyCredential credential = new SharedKeyCredential(accountName, accountKey);
BlobServiceClient blobStorageClient = new BlobServiceClientBuilder()
.endpoint(endpoint)
.credential(credential)
.buildClient();
return blobStorageClient;
}
public static void main(String[] args) throws java.lang.Exception{
task1();
task2();
task3a();
task3b();
task3c();
task4();
task5();
task6();
task7();
task8();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment