https://github.com/edgar-code-repository/spring-boot-with-azure-storage
Rest API that retrieves files from a blob storage container in Azure.
https://github.com/edgar-code-repository/spring-boot-with-azure-storage
Last synced: 12 months ago
JSON representation
Rest API that retrieves files from a blob storage container in Azure.
- Host: GitHub
- URL: https://github.com/edgar-code-repository/spring-boot-with-azure-storage
- Owner: edgar-code-repository
- Created: 2024-08-15T22:44:38.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-01T04:06:58.000Z (almost 2 years ago)
- Last Synced: 2025-01-23T04:29:23.027Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 600 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
SPRING BOOT WITH AZURE STORAGE
---------------------------------------------------------------------------
**Spring Boot REST API that retrieves files from an Azure Storage account.**
---------------------------------------------------------------------------
**Gradle dependency used to work with Azure:**
```
implementation 'com.azure.spring:spring-cloud-azure-starter-storage'
```
---------------------------------------------------------------------------
**Configuration class that generates the instance of BlobServiceClient
needed to interact with the storage container:**
```
package com.example.demo.configuration;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BlobStorageConfiguration {
@Value("${connectionString}")
private String connectionString;
@Bean
public BlobServiceClient getBlobServiceClient() {
return new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
}
}
```
**The connection string used in the previous class is retrieved from an environment variable
(application.properties):**
```
connectionString=${STORAGE_CONNECTION_STRING}
```
---------------------------------------------------------------------------
**A few files were uploaded to an Azure Blob Container named "blob-container":**

**A list of those files is read in this endpoint:**

---------------------------------------------------------------------------
**Code snippet that shows how to retrieve the list of elements inside the blob container:**
```
List filesList = new ArrayList<>();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
for (BlobItem blobItem: blobContainerClient.listBlobs()) {
filesList.add(blobItem.getName());
}
```
---------------------------------------------------------------------------
**Content of file "countries_1.csv" is retrieved as Base64 from container "blob-container":**

---------------------------------------------------------------------------
**Code snippet that shows how to retrieve the content of the file as Base64:**
```
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
BlobClient blobClient = blobContainerClient.getBlobClient(filePathAndName);
if (blobClient.exists()) {
byte[] fileContent = blobClient.downloadContent().toBytes();
String fileContentAsBase64 = Base64.getEncoder().encodeToString(fileContent);
}
```
---------------------------------------------------------------------------
**A new blob container called "new-container" is created:**

**Then, a file called "countries.csv" is sent to the container just created:**

**Now, the file uploaded is available in the new blob container:**

---------------------------------------------------------------------------
**Code snippet to upload the content of the file as Base64:**
```
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
BlobClient blobClient = blobContainerClient.getBlobClient(filePathAndName);
byte[] decodedBytes = Base64.getDecoder().decode(fileContent);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decodedBytes);
blobClient.upload(byteArrayInputStream);
```
---------------------------------------------------------------------------