Comments (8)
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:
- Was this upload was done after the expiry time?
- 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.
- Does the SAS give you write permissions?
- 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.
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 :
Thanks
from azure-storage-android.
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.
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.
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.
YES ! It works !
Thank you very much. If I may, I think this could be specified in the doc :)
from azure-storage-android.
Glad you got it. What would've made it clearer for you?
from azure-storage-android.
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)
- BlobListingDetails missing enum ALL?
- Problem uploading a file
- Unable to resolve hos
- Slow speed download
- CloudBlockBlob generated a wrong SAS token
- Random Non-Fatal Exceptions
- Outdated Sample
- Can there be a kotlin version of the sample compatible with latest azure storage version. HOT 1
- java.lang.ClassNotFoundException: Didn't find class "com.fasterxml.jackson.core.JsonFactory" on path: DexPathList
- SAS token blob container Shared Key authentication HOT 4
- Unexpected attempt to get register for a value without a register HOT 8
- Not found any example to upload a video to Blob.
- Update the Library and Simples
- CloudBlobContainer authentication error when repeating listBlobs() HOT 1
- Closeable resource has leak
- Unavailable ".withClientClaims()" method.
- Azure blob storage setup query
- Changing service endpoint for China
- This repo is missing important files
- why weak crypto ("MD5") using in this library
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-storage-android.