Append text to BlobItem in Azure BlobStorage SDK Java V8

The point was that Append could not be done unless the BlobItem TYPE was created as an APPEND BLOB.

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 :Created as Append Blob
					cab.createOrReplace();
				}
				BlobRequestOptions options = new BlobRequestOptions();
				options.setAbsorbConditionalErrorsOnRetry(true);
				//Error if BLOB TYPE is not 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 {

		}
	}
}

Recommended Posts

Append text to BlobItem in Azure BlobStorage SDK Java V8
Get Timestamp with Azure BlobStorage Java SDK
Upsert from Java SDK to Azure Cosmos DB
Select * from Java SDK to Azure Cosmos DB
Azure functions in java
Delete All from Java SDK in Azure Cosmos DB
Android-Upload image files to Azure Blob Storage in Java
Create Azure Functions in Java
Easily read text files in Java (Java 11 & Java 7)
Multithreaded to fit in [Java] template
Run Java application in Azure Batch
How to learn JAVA in 7 days
[Java] Do not use "+" in append!
Implement reCAPTCHA v3 in Java / Spring
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
How to concatenate strings in java
Delete documents that don't have PK in Azure Cosmos DB (Java SDK)
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Java reference to understand in the figure
HA/DR support in Couchbase (Java SDK commentary)
How to do base conversion in Java
Convert SVG files to PNG files in Java
How to implement coding conventions in Java
Add Document to Azure Search Service (Java)
How to get the date in java
Add footnotes to Word documents in Java
Creating Java Web Applications to Azure Web Apps
[Java Bronze] 5 problems to keep in mind
Call Visual Recognition in Watson Java SDK
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Add SameSite attribute to cookie in Java
Try adding text to an image in Scala using the Java standard library