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