How to delete a file in Firebase Storage using firebase admin sdk - node.js

I''m developing an android app, a web server using flask and firebase.
When client app uploads an image file, the web server saves an image url in database.
And then, client app gets the image url and open image from firebase storage.
So my web server, and app don't know file name.
However, to delete a file in storage,
Knowing the file's name is needed.
Only what I can get is file url.
How can my web server delete a file using file url?
The following code is for deleteting file using filename.
I wanna change this code to deleteting file using file url.
bucket = storage.bucket('<BUCKET_NAME>')
def deleteFile(imageName):
try:
bucket.delete_blob('profile_images/' + imageName)
bucket.delete()
return True
except Exception as e:
print(e)
return False

The Storage client in the Firebase Admin SDK is a thin wrapper around the Node.js client for Google Cloud Storage. And as far as I know the latter doesn't have a way to map the download URL back to a File.
This means that you'll have to find the path from the client using FirebaseStorage.getReferenceFromUrl(), and then pass that path to your web server. That way the JavaScript code can use the path to create a reference to the File.

Related

Can we read files from server path using any fs method in NodeJs

In my case I need to read file/icon.png from cloud storage/bucket which is a token base URL/path. Token resides in header of request.
I tried to use fs.readFile('serverpath') but it gave back error as 'ENOENT' i.e. 'No such file or directory' is existed, but file is existed on that path. So are these methods are eligible to make calls and read files from server or they work only with static path, if that is so then in my case how to read file from cloud bucket/server.
Here i need to pass that file-path to UI, to show this icon.
Use this lib to handle GCS operations.
https://www.npmjs.com/package/#google-cloud/storage
If you do need use fs, install https://cloud.google.com/storage/docs/gcs-fuse, mount bucket to your local filesystem, then use fs as you normally would.
I would like to complement Cloud Ace's answer by saying that if you have Storage Object Admin permission you can make the URL of the image public and use it like any other public URL.
If you don't want to make the URL public you can get temporary access to the file by creating a signed URL.
Otherwise, you'll have to download the file using the GCS Node.js Client.
I posted this as an answer as it is quite long to be a comment.

Files transfered via FTP to Azure app services are not accessible with URLS

I have deployed an ASP.NET CORE web API project on Azure app services. I have copied a file using an FTP client to /site/wwwroot. Now let suppose file name is xyz.jpg, so it should be accessible with link somename.azurewebsites.net/xyz.jpg but ITS NOT. I have tried pasting the file in other folders to but nothing works.
I also have a controller for uploading pictures. It's also working fine. It uploads the picture in desired folder, i can see the picture via FTP client but still the picture is not accessible via any link. What am I doing wrong here ?
For a Web API application, you have to define the request and response yourself in the controller, or your link can't be recognized by the application.
For example, you can add the method to your controller. It works on my side.
[Route("myroute/{pic}")]
public IActionResult Get(string pic)
{
Byte[] b = System.IO.File.ReadAllBytes("image/"+pic);
return File(b, "image/jpeg");
}
In my code, pictures are stored in the folder called image in the root directory, and I define a route called myroute.
Here's my link to access the picture.https://myappname.azurewebsites.net/myroute/mypicname.jpg
Hope it helps.

Node.js: multi-part file upload via REST API

I would like to upload invoking a REST endpoint in multi-part.
In particular, I am looking at this API: Google Cloud Storage: Objects: insert
I did read about using multer, however I did not find any complete example showing me how to perform this operation.
Could someone help me with that?
https://cloud.google.com/nodejs/getting-started/using-cloud-storage#uploading_to_cloud_storage
^^ this is a a good example of how to use multer to upload a single image to Google Cloud Storage. Use multer to create filestream for each file ( storage: multer.memoryStorage() ), and handle the file stream by sending it to your GCS bucket in your callback.
However link only shows an example for one image. If you want to do an array of images, create a for-loop, where you create a stream for each file in your request, but only put the next() function after the for loop ends. If you keep the next(); in each loop cycle you will get the error: Error: Can't set headers after they are sent.
There is an example for uploading files with the nodejs client library and multer. You can modify this example and set the multipart option:
Download the sample code and cd into the folder:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples/
cd nodejs-docs-samples/appengine/storage
Edit the app.yaml file and include your bucket name:
GCLOUD_STORAGE_BUCKET: YOUR_BUCKET_NAME
Then in the source code, you can modify the publicUrl variable according to Objects: insert example:
const publicUrl = format(`https://www.googleapis.com/upload/storage/v1/b/${bucket.name}/o?uploadType=multipart`);
Download a key file for your service account and set the environment variable:
Go to the Create service account key page in the GCP Console.
From the Service account drop-down list, select New service account.
Input a name into the Service account name field.
From the Role drop-down list, select Project > Owner.
Click Create. A JSON file that contains your key downloads to your computer. And finally export the environment variable:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key/file
After that, yo're ready to run npm start and go to the app's frontend and upload your file:

Can't establish the URL of Bot Builder files stored in Azure App Service

I am developing a chatbot using NodeJS and BotBuilder. I have the file chatbot.jpg stored in a folder named image. This sits just beneath the root directory. I am able to display the image (using Kudu to find the URL) in a web browser as follows:
https://mysite.scm.azurewebsites.net/api/vfs/site/wwwroot/images/chatbot.jpg
If I remove the .scm element within the URL, I can no longer display the image.
Even more curiously, if I use the above URL in my bot code, the image doesn't display.
var welcomeCard = new builder.HeroCard(session)
.title("This is the new")
.subtitle('Virtual Assistant')
.images([
new builder.CardImage(session)
.url("http://mysite.scm.azurewebsites.net/api/vfs/site/wwwroot/images/chatbot.jpg")
alt("Virtual Assistant")
]);
session.send(new builder.Message(session)
.addAttachment(welcomeCard));
My question is, how do I find out the regular URL of the image stored in the Azure App Service, so that I can use it in my code?
From the public web, the URL should be https://mysite.azurewebsites.net/images/chatbot.jpg
The wwwroot folder is the root folder served by the app service.
Your code can't just the scm URL as that URL requires you to be logged in to the Azure portal; it's an admin URL.
To accomplish your goal, you need to configure your Restify server to serve static files.
Example Restify config (add to your bot code):
server.get(/\/images\/?.*/, restify.serveStatic({
directory: './images'
}));

DLFileEntryLocalServiceUtil.getFileAsStream(...) not working in liferay server

DLFileEntryLocalServiceUtil.getFileAsStream(...) not working in my server I have s3 configuration for document and media. This method perfectly working without any issue in local server where s3 not configured. Does this require any extra configuration.
I basically want to convert DLFileEntry into File so I had used another method.
File file = DLFileEntryLocalServiceUtil.getFile(user.getUserId(),dlFileEntry.getFileEntryId(), dlFileEntry.getVersion(), true);

Resources