Uploading Images from Google Drive to Contentful using Management API

Has anyone come across any issues uploading images from Google drive to Contentful via the Content Management API. I was able to successfully upload other images hosted online. I am getting an asset processing timeout error while trying to upload from Google Drive.

image

Hey @JBender,

Could you provide a little bit more information as to how you’re uploading these images from Google drive to Contentful?

What is the structure of the requests that you’re sending? (please omit any tokens, identifiers or credentials in your reply)

Hey Gabriel,

The image URL is passed through the file param, data contains additional info such as the type etc.

/**
 * Create a new Contentful Asset
 * @param {*} data Data to create new entry with
 * @param {*} file File to upload
 * @returns {promise<bluebird>}
 */
function uploadAssetFromURL(data, file){
    return client.getSpace(config.contentful.spaceId)
    .then(function(space){
        return space.createAsset({ 
            fields:{
                title: {
                    'en-US': data.title
                },
                file: {
                    'en-US': {
                        contentType: data.type,
                        fileName: data.fileName,
                        upload: file
                    }
                }
            }
        })
        .then(function(asset){
            return asset.processForLocale('en-US');
        })
        .then(function(asset){
            return asset.publish();
        })
        .then(function(asset){
            return promise.resolve(asset.sys.id);
        })
        .catch(function(err){
            console.log(err);
        });
    })
    .catch(function(err){
        console.log(err);
    });
}

Hej @JBender,

I’m not a Google Drive expert at all, but as far as I’m aware, the files in there are only available when you’re logged in. (Apparently it used to be different, but was changed - here’s the Google announcement)

If that’s the case, our Management API won’t be able to fetch the image from Google’s servers, and you’ll need to find a different way to provide the files’ content.

For example, you could use the (beta) Upload API, which is also possible via the JS SDK:

const uploadStream = createReadStream('path/to/filename_english.jpg')
client.getSpace('<space_id>')
.then((space) => space.createUpload({file: uploadStream, 'image/png'})

I haven’t tried, but with the Google Drive Offline Sync feature, you should be able to provide the path to the file within the Google Drive folder, and the SDK could pick it up there and create an Asset from it.

Best,
Stephan

Thank you very much for the reply.

I will take a look at the documentation. I was hoping to use drive as it makes things a little handier but we can also go a different route!

Depending on your use-case this might still be possible. What’s the purpose of using Google Drive? Having multiple people share images into a folder that should automatically be uploaded to Contentful?

You could create a small AWS Lambda / Google Cloud function that only accepts calls from Contentful and has an access token configured internally that is able to read images in said folder via Drive REST API.

Of course this is heavily dependent on your setup, use-case & security restrictions, but just as an idea :slightly_smiling_face:

I ended up finding a way around this all by rebuilding the URLs in the following format:

https://drive.google.com/uc?id=FILE_ID

This worked properly for me with the createAsset() function

As a side note though, the files must either be set to publicly view/download as and/or be located in a folder with similar privileges.