2

I've uploaded an image to Cloud Storage , but I'm having an issue with displaying an image. On the Storage overview, I'm getting Error loading preview.

I haven't got to the step when I add the image link to an object in Cloud Firestore, but I wasn't sure if this is just due to the file type (HEIC, but image/jpeg) or if something is wrong with my code:

Storage Overview:

enter image description here

Image (When clicked on):

Showing question mark.

enter image description here

Image Response (react-native-image-picker):

 {height: 3024, origURL: "assets-library://asset/asset.HEIC?id=CC95F08C-88C3-4012-9D6D-64A413D254B3&ext=HEIC", fileName: null, data: "/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAA…c4z+P04wa+twuG5Yq628/OR1QqSdtLLqrrTe32L79l63P/9k=", width: 4032, …}
height: 3024
origURL: "assets-library://asset/asset.HEIC?id=CC95F08C-88C3-4012-9D6D-64A413D254B3&ext=HEIC"
fileName: null
data: "/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAA"
width: 4032
type: "image/jpeg"
fileSize: 13712705
isVertical: false
uri: "file:///Users/jefflewis/Library/Developer/CoreSimulator/Devices/6B2E3DBA-217F-46B5-AAF2-C1AA540B408E/data/Containers/Data/Application/4CD01B23-6649-4222-BF0E-CB5959732A3E/Documents/images/BECF69B3-9AFF-4CBB-9648-331EF623271C.jpg"
__proto__: Object

Code:

// Image URI
const imageURI = action.newRecipe.image.uri;

// Image Type
const imageType = action.newRecipe.image.type;

// Image Blob
const blob = new Blob([imageURI], {type : `${imageType}`});
console.log('Adding Blob');
console.log(blob.data);

// Firebase Cloud Storage File Path Ref (UUID Generates Randomized File Name)
const filePathRef = imagesRef.child(uuidv4());

// Metadata
const metadata = {
  contentType : `${imageType}`,
};

// Upload File
console.log('Firebase: Uploading image');
const uploadTask = reduxSagaFirebase.storage.uploadFile(filePathRef, blob, metadata);

// Upload Task
uploadTask.on('state_changed', (snapshot) => {
  // Progress
  const progress = (snapshot.bytesTransferred * 100) / snapshot.totalBytes;
  console.log(`Uploading Image: ${progress}%`);
});

// Wait For Upload To Complete
yield uploadTask;
jefelewis
  • 1,850
  • 1
  • 27
  • 63
  • If you see an error in the Firebase console that doesn't come with an explanation, please contact Firebase support with your detail on how to reproduce the issue. https://support.google.com/firebase/contact/support – Doug Stevenson Mar 19 '20 at 03:18
  • Try accessing with another network. Sometimes there are problems with internet providers. – hemauricio Mar 20 '20 at 02:03
  • @jefelewis Can you share your solution? – Byusa Jan 24 '21 at 20:51
  • @Byusa To be honest, the images was the final pain I the ass for Firebase for me and I ended up switching to AWS (AWS Amplify) – jefelewis Feb 14 '21 at 01:48
  • @jefelewis I feel you. Check my solution below with "blob", it fixes it. – Byusa Feb 14 '21 at 02:52

3 Answers3

5

Can you see an Access token if you click the File location section there in your Console? If not, this could be an issue with the fact that your file does not contain an "Access token".

Try adding one in the metadata and see how it goes.

const metadata  = {
  contentType : `${imageType}`,
  firebaseStorageDownloadTokens: uuidv4() //In this line you are adding the access token
};

See, whenever you upload an image using Firebase Console, an access token will be automatically generated. However, if you upload an image using any Admin SDK or gsutil you will need to manually generate this access token yourself.

This curious fact seems to affect the preview in the Firebase Console.

For example, I uploaded the same image using the Admin SDK and the Console and here are the results.

Uploaded using Admin SDK (It keeps loading forever): No access token

Uploaded using the Console (Has an access token in the File location section) enter image description here

I am aware that this error looks a little bit different but could be related. Good luck!

I addressed this issue in this question for Python

hemauricio
  • 872
  • 6
  • 14
  • The code in the question is not using the admin SDK, however. It's using a client SDK, which should work fine. When you use the Admin SDK the preview just doesn't display at all. What you see in the question here is an error message. – Doug Stevenson Mar 19 '20 at 03:16
  • I'm generating an Access token, but not sure what that does (And have the ability to revoke) + Storage location. Still learning Firebase Storage. – jefelewis Mar 19 '20 at 03:46
  • 1
    I managed to get it working following your answer in combination with https://stackoverflow.com/a/43764656/3118542; the nested `metadata` inside `metadata` made the difference. – Toine Heuvelmans Jul 06 '20 at 14:04
  • Github issue related to this: https://github.com/firebase/firebase-admin-node/issues/156 – Anthony Bobenrieth Jan 20 '21 at 22:27
  • @ToineHeuvelmans Can you share your solution? – Byusa Jan 24 '21 at 20:52
  • @Byusa It is like above, but you have to encapsulate `metadata` in another `metadata` object as shown here: https://stackoverflow.com/a/43764656/3118542. – Toine Heuvelmans Jan 25 '21 at 09:50
4

This is how I was able to fix it: Turn your file into blob and it will be fixed, check the code below:

const uploadImage = async () => {
    const response = await fetch(file.uri)
    const blob = await response.blob();
    var ref = firebase.storage().ref().child("FolderName");
    return ref.put(blob)
}
Byusa
  • 2,279
  • 1
  • 16
  • 21
2

You can try this.

let newImageUri

try {
  const response = await fetch(imageUrl)
  const blob = await response.blob()
  await firebase.storage().ref().child(fileName).put(blob)
  var ref = firebase.storage().ref().child(fileName).put(blob)
  newImageUri = await ref.snapshot.ref.getDownloadURL()
    
} catch (error) {
    console.log(error)
    
}