0
votes

i have tried the link and this the but it still doesnt work i dont know why CloudStorageAccount account =new CloudStorageAccount(st, true); CloudBlobClient blobClient = account.createCloudBlobClient(); CloudBlobContainer container = blobClient.getContainerReference(uid); container.createIfNotExists(); BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER); container.uploadPermissions(containerPermissions); File fl = new File(fileUri.getPath());

            CloudBlockBlob blob1 =container.getBlockBlobReference(fl.getName());
            blob1.upload(new FileInputStream(fl), fl.length());

While trying to upload an image to blob storage i keep getting can't connect to ****.blob.core.windows.net

try {
    //Get the rocket data
    FileInputStream fis = new FileInputStream(mFilePath);
    int bytesRead = 0;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];
    while ((bytesRead = fis.read(b)) != -1) {
        bos.write(b, 0, bytesRead);
    }
    byte[] bytes = bos.toByteArray();
    fis.close();

    // Post our rocket data (byte array) to the server
    URL url = new URL(mBlobUrl.replace("\"", ""));
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("PUT");

    urlConnection.addRequestProperty("Content-Type", "image/jpeg");

    // Write image data to server
    DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
    wr.write(bytes);
    wr.flush();``
    wr.close();
    int response = urlConnection.getResponseCode();

    //If we successfully uploaded, return true
    if (response == 201
            && urlConnection.getResponseMessage().equals("Created")) {
        return true;
    }
} catch (Exception ex) {
    Log.e(TAG, ex.getMessage());
}

(@ just formatted to code to show properly)

2

2 Answers

0
votes

You can't upload a file to Blob Storage directly like the way you're doing in your code. By default access to blob storage is restricted to account owners only and you have to consume Azure Storage REST API to interact with it. Essentially you have to invoke Put Blob operation to upload files into blob storage.

See this example for uploading a file using Java SDK for Azure: http://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-blob-storage/#UploadBlob.

0
votes

The sample code will look like below:

CloudStorageAccount account = new CloudStorageAccount(st, true); 
CloudBlobClient blobClient = account.createCloudBlobClient(); 
CloudBlobContainer container = blobClient.getContainerReference(uid);
container.createIfNotExists();
CloudBlockBlob blob = container.getBlockBlobReference("filename");
blob.uploadFromStream(new FileInputStream(mFilePath));

In the below statement of your code what does fl.getName() return. Please make sure it does not contain any invalid characters like '/', '\', etc.

CloudBlockBlob blob1 =container.getBlockBlobReference(fl.getName());