Upload de blob de bloco de armazenamento do Azure do exemplo do android
Estou usando o código a seguir de um aplicativo Android para carregar um blob no Armazenamento de Blob do Azure. Note osasUrl
O parâmetro abaixo é um URL assinado adquirido do meu serviço da web:
// upload file to azure blob storage
private static Boolean upload(String sasUrl, String filePath, String mimeType) {
try {
// Get the file data
File file = new File(filePath);
if (!file.exists()) {
return false;
}
String absoluteFilePath = file.getAbsolutePath();
FileInputStream fis = new FileInputStream(absoluteFilePath);
int bytesRead = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((bytesRead = fis.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
fis.close();
byte[] bytes = bos.toByteArray();
// Post our image data (byte array) to the server
URL url = new URL(sasUrl.replace("\"", ""));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(15000);
urlConnection.setReadTimeout(15000);
urlConnection.setRequestMethod("PUT");
urlConnection.addRequestProperty("Content-Type", mimeType);
urlConnection.setRequestProperty("Content-Length", "" + bytes.length);
urlConnection.setRequestProperty("x-ms-blob-type", "BlockBlob");
// Write file data to server
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.write(bytes);
wr.flush();
wr.close();
int response = urlConnection.getResponseCode();
if (response == 201 && urlConnection.getResponseMessage().equals("Created")) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
O código está funcionando bem para pequenos blobs, mas quando um blob atinge um determinado tamanho, dependendo do telefone com o qual estou testando, começo a receber exceções de memória insuficiente. Gostaria de dividir os blobs e enviá-los em blocos. No entanto, todos os exemplos que encontro na web são baseados em C # e estão usando a biblioteca Storage Client. Estou procurando um exemplo de Java / Android que carrega um blob em blocos usando a API de Descanso de Armazenamento do Azure.