Issue
I'm working on a way to pass multiple files (in one go) to the firebase storage. To do so, I'm using the putFile()
method. This method works perfectly with 1 file, but I'm struggling to get it to work for multiple files (that are stored in an ArrayList
) at a time. Below is the logic I have used (I know it's wrong. Any help is much appreciated):
- Store files that I want to pass to the storage folder in an
ArrayList
- Using a
for-loop
, iterate through the saidArrayList
and put items at[i]
--changes - In an
OnSuccessListener
for 2,downloadUrl
- Initialise an
ArrayList
to store thedownloadUrl
result - In an
OnSuccessListener
for 3, add the downloaded url from 3 into an updatedArrayList
, only if the current position (i) is ==ArrayList.size-1
. I use this so that there aren't any duplicated instances every single time that the method is called (i.e. when the loop runs), but the method is only called when the first 4 (above) are done - Call a method that uploads the
ArrayList
generated from 5 (+ other things) to a rtdb
Below is the code I have written:
for (i in 0 until arrayList.size){
storageRef.putFile(arrayList[i])
.addOnSuccessListener {
Log.d("TAG", "Successfully uploaded file: ${it.metadata?.path}")
storageRef.downloadUrl.addOnSuccessListener { it ->
it.toString()
Log.d("TAG", "File location: $it")
updatedArrayList.add(it.toString())
if (i == arrayList.size){
//method that creates an instance of an object and passes data to the rtdb (firebase)
createInstanceUploadToFirebase(id, updatedArrayList)
}
}
}
}
The issue with the above is that I now get the following error when I try to access (load the file using the generated downloadUrl
) the files (apart from the last one) using the downloadUrl
and only seem to have access to the item at the very last index:
{
"error": {
"code": 403,
"message": "Permission denied."
}
}
Any help is appreciated(:
Solution
The issue wasn't with the logic, it was to do with the security rules for the storage. This is how I resolved the problem:
Update storage security rules to:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow write: if request.auth != null;
allow read;
}
}
}
Reason for the error detailed in the question: The default read and write rules only allow users to read from the storage if they're authenticated. Changing this by using the above storage rules should fix the issue.
Answered By - callMe_whatever
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.