Search code examples
c#firebasewinformsfirebase-storage

Firebase Bucket


I'm trying to upload the Image on Firebase Storage but I face some problems with it.

ar storage = StorageClient.Create();
               
                string studentImageRef = bucket + 33333 + ".jpg";

                var obj = storage.GetObject(studentImageRef, studentImageRef);
                var downloadUrl = obj.MediaLink;

                await storage.UploadObjectAsync(obj, null, new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.PublicRead });

                // Add the download URL to the student's information
                studentsInfo.ImageUrl = downloadUrl;

This is the error I encountered.

Error: The service storage has thrown an exception. HttpStatusCode is NotFound. The specified bucket does not exist

And yes I tried several times and make sure the bucket name is correct but the error still persists.


Solution

  • I figured how to fix this problem using this codes

    / Convert image to stream
                    var stream = new MemoryStream();
                    imageFile.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    stream.Position = 0;
    
                    // Initialize Google Cloud Storage
                    var storage = StorageClient.Create();
    
                    string studentImageRef = "my-bucket.appspot.com/" + Lname_FnameGetter + ".jpg";
    
                    await storage.UploadObjectAsync("my-bucket.appspot.com/", studentImageRef, "image/jpeg", stream, new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.PublicRead });
    
                    // Get the download URL for the image
                    var obj = storage.GetObject("my-bucket.appspot.com", studentImageRef);
                    var downloadUrl = obj.MediaLink;
    
                    // Add the download url to the student's information
                    studentsInfo.ImageUrl = downloadUrl;
    

    The ff. codes i provide solve the problem.