Giter Site home page Giter Site logo

Comments (8)

emgerner-msft avatar emgerner-msft commented on June 23, 2024

First of all, I think I found in the sample the code you're trying to use but it doesn't look too similar to what you wrote above. If you double check that sample, the SAS token is a separate param from the blob URL. The a SAS token shouldn't even have "/" in it -- it's just a query value. So, why are you splitting the SAS and putting in it in the credentials? Is this a URL or just a plain SAS token? The URL passed to CloudBlockBlob should just be the plain old URL to the blob, and the credentials should just take the SAS token (not the full URL).

This first part is likely where things are messed up, but also check the SAS token itself:

  1. Was this upload was done after the expiry time?
  2. If start time was given (usually unnecessary), was this request done before or close to the start time? If so, check for clock skew on the dev box and/or don't specify start time if you can control that.
  3. Does the SAS give you write permissions?
  4. Does the SAS give you access to the blob you're trying to access, or the container the blob is in.

from azure-storage-android.

thib-rdr avatar thib-rdr commented on June 23, 2024

Hi, thanks for the answer.
1) Was this upload was done after the expiry time?

Yes, it is done a few ms after getting the SAS.
2) If start time was given (usually unnecessary), was this request done before or close to the start time? If so, check for clock skew on the dev box and/or don't specify start time if you can control that.

I dont specify a start time.

3) Does the SAS give you write permissions?

Yes, because when I upload "by hand", it works.

4) Does the SAS give you access to the blob you're trying to access, or the container the blob is in.

Same answer as 3), when I upload with code right underneath it works.

try {
                //Get the rocket data
                FileInputStream fis = new FileInputStream(file.getFullFilePath());
                int bytesRead = 0;
                MyByteArrayOutputStream bos = new MyByteArrayOutputStream();
                byte[] b = new byte[1024];
                while ((bytesRead = fis.read(b)) != -1) {
                    final int copy = bytesRead;
                    bos.write(b, 0, bytesRead);
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ViewHelper.updateSyncDialogUpload(status, copy);
                        }
                    });
                }
                byte[] bytes = bos.toByteArray();
                fis.close();
                length = bytes.length;
                URL url = new URL(sas);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setConnectTimeout(Consts.AZURE_CONNECTION_TIMEOUT);
                urlConnection.setReadTimeout(Consts.AZURE_CONNECTION_TIMEOUT);
                urlConnection.setRequestMethod("PUT");
                urlConnection.addRequestProperty("Content-Type", file.getMimeType());
                urlConnection.setRequestProperty("Content-Length", "" + length);
                urlConnection.setRequestProperty("x-ms-blob-type", "BlockBlob");
                Log.d(TAG, "Upload started " + file.getFilename() + " " + file.getMimeType());
                // Write image data to server
                DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
                wr.write(bytes);
                wr.flush();
                wr.close();
                int response = urlConnection.getResponseCode();
                Log.w(TAG, "Response code " + response);
                //If we successfully uploaded, return true
                if (response == 201
                        && urlConnection.getResponseMessage().equals("Created")) {
                    return true;
                }
            } catch (Exception ex) {
                ErrorManager.crash(TAG, ex);
                System.gc();
            }

My sas looks like that :

https://myapp.blob.core.windows.net:443/projectea8fafab-36b8-41c7-9bb9-33d5d9032f7f/a7faba58-7df0-446b-8b13-de78e56ef726/filename.mp4?se=2016-03-25T19%3A47%3A34Z&sp=w&sv=2014-02-14&sr=b&sig=kP%2BDtcV6Zlr8PhxD9aREb7CGzjkRRgTavwXp5WpY3gU%3D

Thanks

from azure-storage-android.

emgerner-msft avatar emgerner-msft commented on June 23, 2024

Good to know the SAS is working! To make progress then please address the first paragraph of my response which goes into the problems with your code using the library.

from azure-storage-android.

thib-rdr avatar thib-rdr commented on June 23, 2024

Hello, and thanks for your answer.

As I was saying, I used the code found here, but the explanation around it are kinda light in my opinion.
I thought that the field imageUri should be the final URL of the image after the upload, but it seems that I was wrong. That is what this snippet is supposed to do :

String[] split = sas.split("/");
URI imageUri = new URI((BuildConfig.API_STORAGE_URL + "/" + split[3] + "/" + split[4] + "/" + file.getGuid().toLowerCase() + "." + file.getExtension()).toLowerCase());

Please remember that my API gives me a SAS looking like that :
https://myapp.blob.core.windows.net:443/projectea8fafab-36b8-41c7-9bb9-33d5d9032f7f/a7faba58-7df0-446b-8b13-de78e56ef726/filename.mp4?se=2016-03-25T19%3A47%3A34Z&sp=w&sv=2014-02-14&sr=b&sig=kP%2BDtcV6Zlr8PhxD9aREb7CGzjkRRgTavwXp5WpY3gU%3D

So my question is, what should I input in the CloudBlockBlob, besides the StorageCredentialsSharedAccessSignature, to make it work ?

CloudBlockBlob blobFromSASCredential = new CloudBlockBlob(imageUri, cred);

Thanks very much for your help :)

from azure-storage-android.

emgerner-msft avatar emgerner-msft commented on June 23, 2024

The reason the constructors in the library take just the SAS token is that our methods to generate SAS tokens return only the token. My first thought is that it would be optimal to return something different in your app. However, I realize that one cannot always change these things.

In general the CloudBlockBlob(URI) constructor is intended for unauthenticated access -- AKA public containers and blobs. However, does work for SAS as well with a couple caveats. First, make sure you use a URI constructor that won't re-encode your URL since it already looks encoded. Second, it removes some extraneous query params, though looking at your URL I don't think this will affect you. So, try just directly constructing a block blob with your full URI including SAS.

CloudBlockBlob blobFromSASCredential = new CloudBlockBlob(new URI(sas));

from azure-storage-android.

thib-rdr avatar thib-rdr commented on June 23, 2024

YES ! It works !

Thank you very much. If I may, I think this could be specified in the doc :)

from azure-storage-android.

emgerner-msft avatar emgerner-msft commented on June 23, 2024

Glad you got it. What would've made it clearer for you?

from azure-storage-android.

emgerner-msft avatar emgerner-msft commented on June 23, 2024

Closing as this looks resolved. Feel free to reopen this for related questions, or open up a new issue if needed.

from azure-storage-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.