LoginSignup
0
0

More than 3 years have passed since last update.

Azure BlobStorage SDK Java V8 で BlobItemにテキストを追記(Append)する

Last updated at Posted at 2019-12-04

BlobItem の TYPE が APPEND BLOBとして作成されていないとAppendできないところがポイントでした。

package hello.azure.blobstorage;

import java.util.Date;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.BlobRequestOptions;
import com.microsoft.azure.storage.blob.CloudAppendBlob;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;

public class WriteData2 {
    // copy from azure console
    public static final String storageConnectionString = "{yourString}";

    public static void main(String[] args) {
        CloudStorageAccount storageAccount;
        CloudBlobClient blobClient = null;
        CloudBlobContainer container = null;

        String containerName = "{yourContainerName}";
        String blobName = "{yourBlobName}";

        try {
            // Parse the connection string and create a blob client to interact with Blob
            // storage
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            blobClient = storageAccount.createCloudBlobClient();
            container = blobClient.getContainerReference(containerName);
            {
                CloudAppendBlob cab = container.getAppendBlobReference(blobName);
                if (cab.exists() == false) {
                    // BLOB TYPE : Append Blob として作成される
                    cab.createOrReplace();
                }
                BlobRequestOptions options = new BlobRequestOptions();
                options.setAbsorbConditionalErrorsOnRetry(true);
                // BLOB TYPE が Append Blob でないとエラーになる
                // Error returned from the service. Http code: 306 and error code:
                // IncorrectBlobType
                cab.appendText("Hello " + new Date() + "\n", "UTF-8", null, options, null);
            }
            System.err.println("finished.");
        } catch (StorageException ex) {
            ex.printStackTrace();
            System.err.println(
                    String.format("HttpStatusCode=%d,ErrorCode=%d", ex.getHttpStatusCode(), ex.getErrorCode()));
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0