Ad

How Can I Pass An Image From Firebase Storage To Facebook Marketing API?

Can someone help me in comprehending how can we download image from Node/Express/Cloud Functions for Firebase.

So write now I am only able to get an obj containing information about my image in firebase storage (through getMetadata();)

let imageInformation = await getImage(domain, thumbID)

where console log of imageInformation would be

[ { kind: 'storage#object',
    id: 'functions-firebase-43a59.appspot.com/outlook.com/assets/d547b001-f5bf-b601-c8b1-4bdeb850995e/1551322039811693',
    selfLink: 'https://www.googleapis.com/storage/v1/b/functions-firebase-43a59.appspot.com/o/outlook.com%2Fassets%2Fd547b001-f5bf-b601-c8b1-4bdeb850995e',
    name: 'outlook.com/assets/d547b001-f5bf-b601-c8b1-4bdeb850995e',
    bucket: 'functions-firebase-43a59.appspot.com',
    generation: '1551322039811693',
    metageneration: '1',
    contentType: 'image/png',
    timeCreated: '2019-02-28T02:47:19.811Z',
    updated: '2019-02-28T02:47:19.811Z',
    storageClass: 'STANDARD',
    timeStorageClassUpdated: '2019-02-28T02:47:19.811Z',
    size: '162416',
    md5Hash: '0nES96sSLfY+8nvoLAdjXQ==',
    mediaLink: 'https://www.googleapis.com/download/storage/v1/b/functions-firebase-43a59.appspot.com/o/outlook.com%2Fassets%2Fd547b001-f5bf-b601-c8b1-4bdeb850995e?generation=1551322039811693&alt=media',
    contentDisposition: 'inline; filename*=utf-8\'\'d547b001-f5bf-b601-c8b1-4bdeb850995e',
    metadata: 
     { firebaseStorageDownloadTokens: '78cc9227-a8e7-4797-9929-37983390ad86' },
    crc32c: 'c5GAZw==',
    etag: 'CO389fi03eACEAE=' },
  { kind: 'storage#object',
    id: 'functions-firebase-43a59.appspot.com/outlook.com/assets/d547b001-f5bf-b601-c8b1-4bdeb850995e/1551322039811693',
    selfLink: 'https://www.googleapis.com/storage/v1/b/functions-firebase-43a59.appspot.com/o/outlook.com%2Fassets%2Fd547b001-f5bf-b601-c8b1-4bdeb850995e',
    name: 'outlook.com/assets/d547b001-f5bf-b601-c8b1-4bdeb850995e',
    bucket: 'functions-firebase-43a59.appspot.com',
    generation: '1551322039811693',
    metageneration: '1',
    contentType: 'image/png',
    timeCreated: '2019-02-28T02:47:19.811Z',
    updated: '2019-02-28T02:47:19.811Z',
    storageClass: 'STANDARD',
    timeStorageClassUpdated: '2019-02-28T02:47:19.811Z',
    size: '162416',
    md5Hash: '0nES96sSLfY+8nvoLAdjXQ==',
    mediaLink: 'https://www.googleapis.com/download/storage/v1/b/functions-firebase-43a59.appspot.com/o/outlook.com%2Fassets%2Fd547b001-f5bf-b601-c8b1-4bdeb850995e?generation=1551322039811693&alt=media',
    contentDisposition: 'inline; filename*=utf-8\'\'d547b001-f5bf-b601-c8b1-4bdeb850995e',
    metadata: 
     { firebaseStorageDownloadTokens: '78cc9227-a8e7-4797-9929-37983390ad86' },
    crc32c: 'c5GAZw==',
    etag: 'CO389fi03eACEAE=' } ]

What I want to achieve?

In facebook Marketing Api, To create an Ad with image, we need to provide it a Image path

curl \
  -F '[email protected]<IMAGE_PATH>' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.11/act_<AD_ACCOUNT_ID>/adimages

Can someone help me in figuring out how I can download or pass the image path to facebook?

Ad

Answer

Implement a HTTPS cloud function that handles the request with the provided information domain and thumbID as follows.

The HTTPS cloud function is nothing but a API endpoint that have to respond with the content data as a webserver would do. So to serve an image you need to send the raw data and set the appropriate HTTP headers (such as Content-Type).

If you're having frequent requests for the same image resource, then you can save yourself from too many Cloud Storage requests and also improve on response time by setting a Cache-Control cache header (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control)

The Admin API for Firebase Cloud Storage is using the GCP API and you can find the documentation for how to download or query files here: https://cloud.google.com/nodejs/docs/reference/storage/2.3.x/File#download

Code sample

Try something like this in your cloud functions index.ts declaration.

import * as admin from 'firebase-admin';

// ...
// Set up your cloud functions etc
// ...

const app = express();
export const tanam = functions.https.onRequest(app);

app.get('/thumb/:domain/:thumbID', async (request, response) => {
    const domain = request.params.domain;
    const thumbID = request.params.thumbID;
    const imageInformation = await getImage(domain, thumbID);
    const contentFile = await admin.storage().bucket().file(imageInformation.name);    
    const [fileContent] = await contentFile.download();

    response.setHeader('Content-Type', imageInformation.contentType);
    response.send(fileContent);

    return null;
});
Ad
source: stackoverflow.com
Ad