Jenna Diop
Jenna Diop

Reputation: 23

How to upload a file in a folder GOOGLE DRIVE API

So i try this, i use my folder ID, but that's doesn't work, my files are create but they don't go in my folder. In the doc of Google Drive there is only this example. When i submit a form on Slack, that's push my data on a Google Spreadsheet and after that's auto generate a ticket on monday. Now i want to push my spreadsheet on a special folder in my Drive.. Can i provide my files in an other way ?

async function createSpreadsheet(filename, body) {
      const ID_FOLDER_DRIVE = MY_ID;
    
      try {
    
        const fileMetaData = {
          'name': filename,
          parents: [ID_FOLDER_DRIVE]
        }
    
        const media = {
          mimeType: 'text/csv',
          body: body
        }
    
        const res = await drive.files.create({
          requestBody: {
            resource: fileMetaData,
            mimeType: 'application/vnd.google-apps.spreadsheet'
          },
          media: media
        })
        return res.data
      } catch (error) {
        throw error
      }
    }
    
    async function generatePublicUrl(id) {
      console.log({ id })
      try {
        await drive.permissions.create({
          fileId: id,
          requestBody: {
            role: 'reader',
            type: 'anyone',
          },
        });
    
        /* 
        webViewLink: View the file in browser
        webContentLink: Direct download link 
        */
        const result = await drive.files.get({
          fileId: id,
          fields: 'webViewLink, webContentLink',
        });
        return result.data.webViewLink
      } catch (error) {
        throw error
      }
    }

Upvotes: 0

Views: 537

Answers (1)

Tanaike
Tanaike

Reputation: 201613

In your situation, how about the following modification?

From:

  const fileMetaData = {
    'name': filename,
    parents: [ID_FOLDER_DRIVE]
  }

  const media = {
    mimeType: 'text/csv',
    body: body
  }

  const res = await drive.files.create({
    requestBody: {
      resource: fileMetaData,
      mimeType: 'application/vnd.google-apps.spreadsheet'
    },
    media: media
  })

To:

const fileMetaData = {
  name: filename,
  parents: [ID_FOLDER_DRIVE],
  mimeType: 'application/vnd.google-apps.spreadsheet'
}

const media = {
  mimeType: 'text/csv',
  body: body
}

const res = await drive.files.create({
  requestBody: fileMetaData,
  media: media
})

Reference:

Upvotes: 1

Related Questions