I would like to upload a file to Azure Blob using a generated SAS URL for the blob but it is failed when I executed the URL. I received HTTP error 400 with the message
An HTTP header that's mandatory for this request is not specified.
Here is my code:
BlobServiceClient blobServiceClient = initBlobStorageClient();
BlobContainerClient testprovenanceContainer = getContainerClient(blobServiceClient, "testcontainer");
BlobClient blobClient = testprovenanceContainer.getBlobClient("hello.png");
OffsetDateTime expiryTime = OffsetDateTime.now().plusMinutes(5);
BlobContainerSasPermission permission = new BlobContainerSasPermission().setAddPermission(true).setWritePermission(true);
BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission)
.setStartTime(OffsetDateTime.now());
String s = "https://testazure.blob.core.windows.net/testcontainer/hello.png?"+blobClient.generateSas(values);
uploadFileWithUrl(new File("hello.png"), new URL(a));
public static void uploadFileWithUrl(File file, URL url) throws IOException {
String contentType = Files.probeContentType(file.toPath());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-type", contentType);
connection.setRequestProperty("User-Agent", "Multi-Cloud Management 006b8507-b815-47b9-bce0-08b91981f17a");
DataOutputStream out = new DataOutputStream (connection.getOutputStream());
out.write(Files.readAllBytes(Paths.get(file.toURI())));
out.close();
// Check the HTTP response code. To complete the upload and make the object available,
// you must interact with the connection object in some way.
connection.getResponseCode();
System.out.println("HTTP response code: " + connection.getResponseCode());
connection.disconnect();
}