Home > Software design >  Getting mimeType "octet stream" for a folder created with API
Getting mimeType "octet stream" for a folder created with API

Time:03-31

I'm creating a folder inside the appDataFolder with the google drive api as it follows:

    const fileMetadata = {
      'name': `folderName`,
      'parents': ['appDataFolder']
    };
    const media = {
      mimeType: 'application/vnd.google-apps.folder',
    };
    const folderCreation = await drive.files.create({
      resource: fileMetadata,
      media: media,
      fields: 'id'
    })

But when I list the files inside the appDataFolder I get a file with the mimeType of "octet stream" instead of "application/vnd.google-apps.folder" as I stated on the creation. This is a problem because, later when I want to create a file inside this folder I created, I can't because of the mime type (I imagine) , and I get the error:

UnhandledPromiseRejectionWarning: Error: The specified parent is not a folder.

So why is my folder receiving a mimetype of octet stream and how can I solve it?

Thanks in advance if you know the solution.

CodePudding user response:

I needed to include the mimeType inside the fileMetaData, as opposed as the documentation says. Simple solution but hard to find if you are strongly following the docs trusting they are right.

CodePudding user response:

You forgot to add the mime type to the metadata. This tells drive what the mime type should be when you upload it.

const fileMetadata = {
      'name': `folderName`,
      'parents': ['appDataFolder'],
      'mimeType' : 'application/vnd.google-apps.folder'
    };

Media is used for uploading files. Using media with a file upload you can tell google what the mime type of the file you are uploading. Then use the mime type to tell them what the type should be once it is uploaded.

For example you could upload a csv file and tell them to convert it to a google sheet.

var fileMetadata = {
  'name': 'My Report',
  'mimeType': 'application/vnd.google-apps.spreadsheet'
};
var media = {
  mimeType: 'text/csv',
  body: fs.createReadStream('files/report.csv')
};
drive.files.create({
  resource: fileMetadata,
  media: media,
  fields: 'id'
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id:', file.id);
  }
});
  • Related