update sample to release version 2.0.7 (#443)
* update sample and dev version after 2.0.7 release
This commit is contained in:
Родитель
5ee3e8f69f
Коммит
b88d13d230
|
@ -5,7 +5,7 @@
|
|||
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-bom</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>Azure Spring Boot BOM</name>
|
||||
|
@ -46,7 +46,7 @@
|
|||
</scm>
|
||||
|
||||
<properties>
|
||||
<azure.spring.boot.version>2.0.6-SNAPSHOT</azure.spring.boot.version>
|
||||
<azure.spring.boot.version>2.0.8-SNAPSHOT</azure.spring.boot.version>
|
||||
<azure.dependencies.bom.version>1.0.0.M4</azure.dependencies.bom.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-bom</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../azure-spring-boot-bom/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-documentdb-spring-boot-starter</artifactId>
|
||||
<artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
|
|
|
@ -5,11 +5,10 @@
|
|||
*/
|
||||
package sample.cloudfoundry.storage;
|
||||
|
||||
import com.microsoft.azure.storage.CloudStorageAccount;
|
||||
import com.microsoft.azure.storage.StorageException;
|
||||
import com.microsoft.azure.storage.blob.CloudBlobClient;
|
||||
import com.microsoft.azure.storage.blob.CloudBlobContainer;
|
||||
import com.microsoft.azure.storage.blob.CloudBlockBlob;
|
||||
import com.microsoft.azure.storage.blob.BlockBlobURL;
|
||||
import com.microsoft.azure.storage.blob.ContainerURL;
|
||||
import com.microsoft.azure.storage.blob.TransferManager;
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -21,11 +20,13 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.AsynchronousFileChannel;
|
||||
|
||||
@SuppressFBWarnings({"RV_RETURN_VALUE_IGNORED"})
|
||||
@RestController
|
||||
public class StorageRestController {
|
||||
|
||||
|
@ -33,8 +34,9 @@ public class StorageRestController {
|
|||
"https://raw.githubusercontent.com/mjeffries-pivotal/pcf-samples/master/images/azure-pcf.jpg";
|
||||
private static final Logger LOG = LoggerFactory
|
||||
.getLogger(StorageRestController.class);
|
||||
|
||||
@Autowired
|
||||
private CloudStorageAccount account;
|
||||
private ContainerURL containerURL;
|
||||
|
||||
@RequestMapping(value = "/blob", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
|
@ -43,36 +45,34 @@ public class StorageRestController {
|
|||
|
||||
try {
|
||||
LOG.info("showBlob start");
|
||||
if (account == null) {
|
||||
LOG.error("Storage Account is null!");
|
||||
if (containerURL == null) {
|
||||
LOG.error("ContainerURL is null!");
|
||||
return;
|
||||
}
|
||||
|
||||
final URL u = new URL(IMAGE_PATH);
|
||||
is = u.openStream();
|
||||
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
|
||||
final int imageSize = IOUtils.copy(is, response.getOutputStream());
|
||||
IOUtils.copy(is, response.getOutputStream());
|
||||
|
||||
LOG.debug("Connecting to storage account...");
|
||||
final CloudBlobClient serviceClient = account
|
||||
.createCloudBlobClient();
|
||||
|
||||
// Container name must be lower case.
|
||||
final CloudBlobContainer container = serviceClient
|
||||
.getContainerReference("myimages");
|
||||
container.createIfNotExists();
|
||||
// Create container.
|
||||
containerURL.create(null, null, null);
|
||||
|
||||
// Upload an image file.
|
||||
LOG.debug("Uploading image...");
|
||||
final CloudBlockBlob blob = container
|
||||
.getBlockBlobReference("image1.jpg");
|
||||
blob.upload(new URL(IMAGE_PATH).openStream(), imageSize);
|
||||
LOG.debug("Uploading image complete");
|
||||
final BlockBlobURL blockBlobURL = containerURL.createBlockBlobURL("image1.jpg");
|
||||
final File imageFile = new File(IMAGE_PATH);
|
||||
final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(imageFile.toPath());
|
||||
|
||||
TransferManager.uploadFileToBlockBlob(fileChannel, blockBlobURL, 8 * 1024 * 1024, null)
|
||||
.subscribe(r -> {
|
||||
LOG.debug("Uploading image complete");
|
||||
}, error -> {
|
||||
LOG.error("Failed to upload image", error);
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
LOG.error("Error retrieving image", e);
|
||||
} catch (URISyntaxException | StorageException e) {
|
||||
LOG.error("Error accessing azure storage container", e);
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-documentdb-spring-boot-starter</artifactId>
|
||||
<artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
|
|
@ -23,6 +23,11 @@
|
|||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-storage-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.findbugs</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>3.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -6,42 +6,81 @@
|
|||
|
||||
package sample.storage;
|
||||
|
||||
import com.microsoft.azure.storage.CloudStorageAccount;
|
||||
import com.microsoft.azure.storage.StorageException;
|
||||
import com.microsoft.azure.storage.blob.CloudBlobClient;
|
||||
import com.microsoft.azure.storage.blob.CloudBlobContainer;
|
||||
import com.microsoft.azure.spring.autoconfigure.storage.StorageProperties;
|
||||
import com.microsoft.azure.storage.blob.BlockBlobURL;
|
||||
import com.microsoft.azure.storage.blob.ContainerURL;
|
||||
import com.microsoft.azure.storage.blob.ServiceURL;
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
|
||||
@SuppressFBWarnings({"RV_RETURN_VALUE_IGNORED"})
|
||||
@SpringBootApplication
|
||||
public class StorageSampleApplication implements CommandLineRunner {
|
||||
private static final String SOURCE_FILE = "storageTestFile.txt";
|
||||
|
||||
@Autowired
|
||||
private CloudStorageAccount cloudStorageAccount;
|
||||
private ServiceURL serviceURL;
|
||||
|
||||
@Autowired
|
||||
private ContainerURL containerURL;
|
||||
|
||||
@Autowired
|
||||
private StorageProperties properties;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(StorageSampleApplication.class);
|
||||
}
|
||||
|
||||
public void run(String... var1) throws URISyntaxException, StorageException {
|
||||
createContainerIfNotExists("mycontainer");
|
||||
}
|
||||
public void run(String... var1) throws IOException {
|
||||
final File sourceFile = new File(this.getClass().getClassLoader().getResource(SOURCE_FILE).getFile());
|
||||
final File downloadFile = Files.createTempFile("azure-storage-test", null).toFile();
|
||||
|
||||
// Note: Here is the minimum sample code that demonstrates how CloudStorageAccount is autowired and used.
|
||||
// For more complete Azure Storage API usage, please go to https://github.com/Azure-Samples and search repositories
|
||||
// with key words `storage` and 'java'.
|
||||
private void createContainerIfNotExists(String containerName) throws URISyntaxException, StorageException {
|
||||
// Create the blob client.
|
||||
final CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient();
|
||||
StorageService.createContainer(containerURL, properties.getContainerName());
|
||||
final BlockBlobURL blockBlobURL = containerURL.createBlockBlobURL(SOURCE_FILE);
|
||||
|
||||
// Get a reference to a container.
|
||||
// The container name must be lower case
|
||||
final CloudBlobContainer container = blobClient.getContainerReference(containerName);
|
||||
System.out.println("Enter a command:");
|
||||
System.out.println("(P)utBlob | (G)etBlob | (D)eleteBlobs | (E)xitSample");
|
||||
final BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8.name()));
|
||||
|
||||
// Create the container if it does not exist.
|
||||
container.createIfNotExists();
|
||||
boolean isExit = false;
|
||||
while (!isExit) {
|
||||
System.out.println("Enter a command:");
|
||||
final String input = reader.readLine();
|
||||
if (input == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(input) {
|
||||
case "P":
|
||||
StorageService.uploadFile(blockBlobURL, sourceFile);
|
||||
break;
|
||||
case "G":
|
||||
StorageService.downloadBlob(blockBlobURL, downloadFile);
|
||||
break;
|
||||
case "D":
|
||||
StorageService.deleteBlob(blockBlobURL);
|
||||
break;
|
||||
case "E":
|
||||
System.out.println("Cleaning up container and tmp file...");
|
||||
containerURL.delete(null, null).blockingGet();
|
||||
FileUtils.deleteQuietly(downloadFile);
|
||||
isExit = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
package sample.storage;
|
||||
|
||||
import com.microsoft.azure.storage.blob.BlobRange;
|
||||
import com.microsoft.azure.storage.blob.BlockBlobURL;
|
||||
import com.microsoft.azure.storage.blob.ContainerURL;
|
||||
import com.microsoft.azure.storage.blob.TransferManager;
|
||||
import com.microsoft.azure.storage.blob.models.ContainerCreateResponse;
|
||||
import com.microsoft.rest.v2.RestException;
|
||||
import com.microsoft.rest.v2.util.FlowableUtil;
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.AsynchronousFileChannel;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
@SuppressFBWarnings({"RV_RETURN_VALUE_IGNORED"})
|
||||
public class StorageService {
|
||||
public static void uploadFile(BlockBlobURL blob, File sourceFile) throws IOException {
|
||||
logInfo("Start uploading file %s...", sourceFile);
|
||||
final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(sourceFile.toPath());
|
||||
|
||||
TransferManager.uploadFileToBlockBlob(fileChannel, blob, 8 * 1024 * 1024, null)
|
||||
.toCompletable()
|
||||
.doOnComplete(() -> logInfo("File %s is uploaded.", sourceFile.toPath()))
|
||||
.doOnError(error -> logError("Failed to upload file %s with error %s.", sourceFile.toPath(),
|
||||
error.getMessage()))
|
||||
.blockingAwait();
|
||||
}
|
||||
|
||||
public static void deleteBlob(BlockBlobURL blockBlobURL) {
|
||||
logInfo("Start deleting file %s...", blockBlobURL.toURL());
|
||||
blockBlobURL.delete(null, null, null)
|
||||
.toCompletable()
|
||||
.doOnComplete(() -> logInfo("Blob %s is deleted.", blockBlobURL.toURL()))
|
||||
.doOnError(error -> logError("Failed to delete blob %s with error %s.",
|
||||
blockBlobURL.toURL(), error.getMessage()))
|
||||
.blockingAwait();
|
||||
}
|
||||
|
||||
public static void downloadBlob(BlockBlobURL blockBlobURL, File downloadToFile) {
|
||||
logInfo("Start downloading file %s to %s...", blockBlobURL.toURL(), downloadToFile);
|
||||
FileUtils.deleteQuietly(downloadToFile);
|
||||
|
||||
blockBlobURL.download(new BlobRange().withOffset(0).withCount(4 * 1024 * 1024L), null, false, null)
|
||||
.flatMapCompletable(
|
||||
response -> {
|
||||
final AsynchronousFileChannel channel = AsynchronousFileChannel
|
||||
.open(Paths.get(downloadToFile.getAbsolutePath()), StandardOpenOption.CREATE,
|
||||
StandardOpenOption.WRITE);
|
||||
return FlowableUtil.writeFile(response.body(null), channel);
|
||||
})
|
||||
.doOnComplete(() -> logInfo("File is downloaded to %s.", downloadToFile))
|
||||
.doOnError(error -> logError("Failed to download file from blob %s with error %s.",
|
||||
blockBlobURL.toURL(), error.getMessage()))
|
||||
.blockingAwait();
|
||||
}
|
||||
|
||||
public static void createContainer(ContainerURL containerURL, String containerName) {
|
||||
logInfo("Start creating container %s...", containerName);
|
||||
try {
|
||||
final ContainerCreateResponse response = containerURL.create(null, null, null).blockingGet();
|
||||
logInfo("Storage container %s created with status code: %s.", containerName, response.statusCode());
|
||||
} catch (RestException e) {
|
||||
if (e.response().statusCode() != 409) {
|
||||
logError("Failed to create container %s.", containerName, e);
|
||||
throw e;
|
||||
} else {
|
||||
logInfo("%s container already exists.", containerName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void logInfo(String log, Object... params) {
|
||||
System.out.println(String.format(log, params));
|
||||
}
|
||||
|
||||
private static void logError(String log, Object... params) {
|
||||
System.err.println(String.format(log, params));
|
||||
}
|
||||
}
|
|
@ -1 +1,3 @@
|
|||
azure.storage.connection-string=put-your-azure-storage-connection-string-here
|
||||
azure.storage.account-name=put-your-azure-storage-account-name-here
|
||||
azure.storage.account-key=put-your-azure-storage-account-key-here
|
||||
azure.storage.container-name=put-your-azure-storage-container-name-here
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
This is a sample file to test Azure storage.
|
|
@ -66,7 +66,7 @@
|
|||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-bom</artifactId>
|
||||
<version>2.0.5</version>
|
||||
<version>2.0.7</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<parent>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-parent</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<relativePath>../azure-spring-boot-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -5,7 +5,7 @@
|
|||
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>azure-spring-boot-build</artifactId>
|
||||
<version>2.0.6-SNAPSHOT</version>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>Azure Spring Boot Build</name>
|
||||
|
|
Загрузка…
Ссылка в новой задаче