In this article, I investigated what you need to do when working with Azure Blob Storage using Java libraries. I will continue to do it.
The libraries used are the following versions.
As a matter of course, it is essential.
Azure Storage Account
From the Azure portal, select Dashboard> Storage Accounts> Create Storage Account Enter the storage account name, etc., and select "Blob Storage" as the account type.
Other parameters can be freely determined according to the application.
This time, we will assume that you created it with the name ** storage-account **.
In Blob Storage, resources are managed in the following units.
For more information, see here (https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction#blob-storage-resources).
This time, we will assume that you created it with the name ** log **.
Once you have a storage account, you can get an access key.
You can get the access key by accessing Storage account> Storage account name> Access key.
The connection string seems to have the following format, so please get it or assemble it yourself.
DefaultEndpointsProtocol=${DefaultEndpointsProtocol};AccountName=${AccountName};AccountKey=${AccountKey};EndpointSuffix=core.windows.net
Now that you're ready, let's get started with the Blob storage.
I will try to get the SAS token.
See below for SAS tokens https://docs.microsoft.com/ja-jp/azure/storage/common/storage-sas-overview
//Use the obtained connection string to get the BlobClient
// BLOB_CONNECTION_For SETING, use the connection string obtained from the Azure portal.
var blobClient = new BlobServiceClientBuilder().connectionString(BLOB_CONNECTION_STRING).buildClient();
//Set the authority to be assigned to SAS.
var permission = AccountSasPermission.parse("rwacd"); // read + write + add + create +Assign delete
var service = AccountSasService.parse("b"); //Specify the type of Storage service you want to use. This time Blob(b)is
var resourceType = AccountSasResourceType.parse("oc"); //Select the resources you want to allow the operation. I want to operate Blob and container, so Object(o≒blob)And Container(c)To specify
var expireTime = OffsetDateTime.now().plusSeconds(10 * 60); //Specifies the expiration date of the generated SAS token. Current time because it must be specified in OffsetDateTime+Let's allocate 10 minutes.
Once this is done, call generateAccountSas on blobClient to generate a SAS token.
//Account Sas that I worked hard to generate~Get AccountSasSignatureValues with arguments such as
var sig = new AccountSasSignatureValues(expireTime, permission, service, resourceType);
//You can get SAS token.
var sasToken = blobClient.generateAccountSas(sig);
As a result, the tokens obtained will be in the following format.
?sv=2019-02-02&ss=b&srt=sco&sp=rwdlac&se=2020-03-21T13:30:24Z&st=2020-03-21T05:30:24Z&spr=https&sig=60eYuGBBTSRAbQ3%2BjNGcpdu63ohWKbPCpCkC%2FSrWuqw%3D
Since it cannot be used as a connection string as it is, connect it to the following format.
BlobEndpoint=${ストレージアカウント}.blob.core.windows.net/;TableEndpoint=${ストレージアカウント}.blob.core.windows.net/;SharedAccessSignature=${sasToken}
You can now use the SAS token as a connection string with a deadline and so on.
Let's perform a storage operation using the SAS token version of the connection string!
The client acquisition method is the same, but the parameters passed to the BlobServiceClientBuilder # connectionString
method use the SAS token version of the connection string.
var SAS_CONNECTION_STRING = "BlobEndpoint=storage-account.blob.core.windows.net/;TableEndpoint=storage-account.blob.core.windows.net/;SharedAccessSignature=?sv=2019-02-02&ss=b&srt=sco&sp=rwdlac&se=2020-03-21T13:30:24Z&st=2020-03-21T05:30:24Z&spr=https&sig=60eYuGBBTSRAbQ3%2BjNGcpdu63ohWKbPCpCkC%2FSrWuqw%3D";
var blobClient = new BlobServiceClientBuilder().connectionString(SAS_CONNECTION_STRING ).buildClient();
Now let's upload the file to the log container.
//Get log container.
var logContainer = blobClient.getBlobContainerClient("log");
//in the log container, access.Upload as an object called log.
var accesslog = logContainer.getBlobClient("access.log");
// access.In the log, "Hello, World!".
try {
var helloWorld = "Hello, World!".getBytes();
accesslog.upload(new ByteArrayInputStream(helloWorld), helloWorld.length);
} catch (Exception e) {
//Something like error handling
}
Besides, it seems that the following methods exist.
accesslog.download(outputStream);
accesslog.delete();
For the time being, it has been summarized to the point where file operations can be performed to some extent.
By the way, if you specify the file name separated by /
at the time of getBlobClient
, it will be treated as a virtual directory.
There are V8 version and V12 version of Java library, but is it different depending on the Java version? I thought, but it seems that the version of the library is simply different. I tried the same process using V8 version, but died with URISyntaxException. It was an easy win if I replaced the library.
Recommended Posts