This commit is contained in:
Yihezkel Schoenbrun 2021-01-04 04:21:32 +02:00 коммит произвёл GitHub
Родитель 013e95f700
Коммит 8cdd4bf3ed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 140 добавлений и 0 удалений

77
pom.xml Normal file
Просмотреть файл

@ -0,0 +1,77 @@
<?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>com.azure.storage.blob.connection.test</groupId>
<artifactId>upload-file-to-azure-blob</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4jVersion>1.6.1</slf4jVersion>
<azureStorageVersion>12.6.0</azureStorageVersion>
<mvnComiplerVersion>3.8.1</mvnComiplerVersion>
</properties>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.azure.storage.blob.connection.test.UploadFile</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.azure.storage.blob.connection.test.UploadFile</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${mvnComiplerVersion}</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>${azureStorageVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
</dependency>
</dependencies>
</project>

Просмотреть файл

@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package com.azure.storage.blob.connection.test;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Optional;
public class UploadFile {
private static final Logger log = LoggerFactory.getLogger(UploadFile.class);
public static void main(String[] args) throws IOException {
String connectionString = System.getenv("connectionString");
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
// Create the container and return a container client object
String containerName = "test-container-" + java.util.UUID.randomUUID().toString().substring(0, 6);
BlobContainerClient containerClient = blobServiceClient.createBlobContainer(containerName);
String fileName = "test-file-" + java.util.UUID.randomUUID() + ".txt";
File localFile = new File(fileName);
try (FileWriter writer = new FileWriter(localFile, true)) {
writer.write("Upload was successful!");
}
// Reference to the blob
BlobClient blobClient = containerClient.getBlobClient(fileName);
// Upload the blob
log.info("Uploading to Azure Blob Storage URL: {}", blobClient.getBlobUrl());
try {
blobClient.uploadFromFile(fileName);
} catch (Exception e) {
log.error("Couldn't upload blob {}", fileName, e);
}
// Enumerate blob in the container
Optional<BlobItem> blobItemOptional = containerClient.listBlobs().stream().findFirst();
BlobItem blobItem;
if (blobItemOptional.isPresent()) {
blobItem = blobItemOptional.get();
if (!fileName.equals(blobItem.getName())) {
log.error("Blob in container was named {}, not {} as expected", blobItem.getName(), fileName);
} else {
log.info("Blob {} was successfully uploaded!", blobItem.getName());
}
} else {
log.error("Blob {} couldn't be retrieved from container", fileName);
}
containerClient.delete();
localFile.delete();
}
}