Storage Upload freezes and times out
[REQUIRED] Describe your environment
- Operating System version: Android 7.1.2
- Firebase SDK version: 4.3.0
- Firebase Product: storage I am running an Expo app (a layer on top of React Native) on my Android phone. The issue only occurs on device, not in emulator. I am definitely connected to the internet, as the app and all other resources load fine.
[REQUIRED] Describe the problem
Attempting to upload an image to Firebase storage produces a storage/retry-limit-exceeded error.
Steps to reproduce:
After calling .put() on a Storage Reference with a Uint8Array, it triggers one 'state_changed' event (0% done, status firebase.storage.TaskState.RUNNING) then after a minute or so times out with the following error:
FirebaseStorageError {
"code_": "storage/retry-limit-exceeded",
"message_": "Firebase Storage: Max retry time for operation exceeded, please try again.",
"name_": "FirebaseError",
"serverResponse_": null,
}
It works for sufficiently small images (a few kilobytes) but for a camera phone photo it fails. It worked fine for about a week after it was implemented and then stopped working.
Console output:
3:04:01 PM: Starting image upload
3:04:01 PM: Byte array loaded 1784526
3:04:01 PM: Database image upload complete
3:04:01 PM: Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 3252366ms)
3:04:01 PM: Upload is 0% done
3:04:01 PM: Upload is running
3:04:01 PM: Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 126226ms)
3:05:03 PM: Unsuccessful upload FirebaseStorageError {
"code_": "storage/retry-limit-exceeded",
"message_": "Firebase Storage: Max retry time for operation exceeded, please try again.",
"name_": "FirebaseError",
"serverResponse_": null,
}
Relevant Code:
I am using direct byte upload instead of file upload because Expo does not have access to the file system. I am not using putString because I could not get base64 data URI's to work properly with Firebase.
console.log('Starting image upload');
const uploadTask = FirebaseUpload(
this.state.base64,
'uploads/' + eventKey + '.jpg'
);
uploadTask.on(
'state_changed',
function(snapshot) {
// Observe state change events such as progress, pause, and resume
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = snapshot.bytesTransferred / snapshot.totalBytes * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
},
function(error) {
// Handle unsuccessful uploads
console.warn('Unsuccessful upload', error);
},
function() {
// Handle successful uploads on complete
console.log('Image upload complete', uploadTask.snapshot.downloadURL);
// Update the info about the post to point to the newly uploaded image
ref
.child(eventKey)
.child('image')
.set(uploadTask.snapshot.downloadURL);
}
);
import * as firebase from 'firebase';
import { decode } from 'base-64';
// Upload the given base64 data (must be jpg) to the given path in firebase
// storage. Path should have jpg extension.
export default function(base64, path) {
let bytes = convertToByteArray(base64);
console.log('Byte array loaded', bytes.length);
return firebase.storage().ref(path).put(bytes, {
contentType: 'image/jpeg'
});
}
// Convert from a base64 string to an array of bytes
function convertToByteArray(input) {
var binary_string = decode(input);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes;
}
Uploads are not currently supported in React Native. See: https://github.com/firebase/firebase-js-sdk/blob/master/ENVIRONMENTS.md
I'm not familiar with Expo, but if it's a layer on top of React Native then this is expected to not work right now.