How to Upload 3 Files to Google Drive with Integration Instead of Just 1 URL?

This topic was automatically generated from Slack. You can find the original thread here.

I try to upload 3 files to google drive with the integration.

The file URL is linke this:
url,url,url

But it seems that it only are working with 1 url and not 3.

I can see Zapier does handle this in their flow.

Do any have a solution to this? :raised_hands:

I try to upload 3 files to google drive with the integration.

The file URL is linke this:
url,url,url

But it seems that it only are working with 1 url and not 3.

I can see Zapier does handle this in their flow.

Do any have a solution to this? :raised_hands:

To upload multiple files to Google Drive using Pipedream, you can modify your workflow to handle multiple URLs. You can use a loop to iterate through the URLs and upload each file one by one. Hereā€™s an example of how you can achieve this: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, fileUrls: { type: "string[]", label: "File URLs", description: "Enter the URLs of the files you want to upload to Google Drive", }, }, async run({ steps, $ }) { const uploadFile = async (url) => { const fileMetadata = { name: url.split("/").pop(), mimeType: "application/octet-stream", }; const media = { mimeType: "application/octet-stream", body: (await axios($, { url, responseType: "stream" })).data, }; return await axios($, { method: "POST", url: "https://www.googleapis.com/upload/drive/v3/files", headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }, params: { uploadType: "multipart", }, data: JSON.stringify(fileMetadata), media, }); }; const uploadedFiles = []; for (const url of this.fileUrls) { const response = await uploadFile(url); uploadedFiles.push(response.data); } return uploadedFiles; }, }); This component accepts an array of file URLs and uploads each file to Google Drive. Make sure to connect your Google Drive account in the google prop when using this component in your workflow.

please update that example to use the google_drive app instead of google.

I apologize for the confusion earlier. Pipedream has pre-built Google Drive actions that you can use to upload files without writing any code. To upload multiple files using the Google Drive actions, you can follow these steps: 1. In your Pipedream workflow, add a new step and search for the ā€œGoogle Driveā€ app. 2. Select the ā€œCreate Fileā€ action. 3. Connect your Google Drive account. 4. For the ā€œFileā€ field, you can use the first URL from your list (e.g., steps.trigger.event.url1 if the URLs are coming from the trigger event). 5. Repeat steps 1-4 for the other two URLs, making sure to update the ā€œFileā€ field with the appropriate URL each time. This will create three separate steps in your workflow, each uploading a file to Google Drive. If you need further assistance or have any questions, feel free to visit https://pipedream.com/support for more support options.

That will only work if I always have exactly 4 files on every workflow execution, and it is repetitive. Can you accomplish this with a single code step?

I apologize for the confusion earlier. Hereā€™s an updated example using a single code step to upload multiple files to Google Drive: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, fileUrls: { type: "string[]", label: "File URLs", description: "Enter the URLs of the files you want to upload to Google Drive", }, }, async run({ $ }) { const uploadFile = async (url) => { const fileMetadata = { name: url.split("/").pop(), mimeType: "application/octet-stream", }; const media = { mimeType: "application/octet-stream", body: (await axios($, { url, responseType: "stream" })).data, }; return await axios($, { method: "POST", url: "https://www.googleapis.com/upload/drive/v3/files", headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }, params: { uploadType: "multipart", }, data: JSON.stringify(fileMetadata), media, }); }; const uploadedFiles = []; for (const url of this.fileUrls) { const response = await uploadFile(url); uploadedFiles.push(response.data); } return uploadedFiles; }, }); This component accepts an array of file URLs and uploads each file to Google Drive. Make sure to connect your Google Drive account in the google prop when using this component in your workflow.

why did you use app: "google" instead of app: "google_drive"?

I apologize for the confusion in my previous response. Youā€™re right, I should have used app: "google_drive" instead of app: "google". Hereā€™s the corrected code snippet: import { axios } from "@pipedream/platform"; export default { name: "Upload Multiple Files to Google Drive", async run({ steps, $ }) { const fileUrls = ["url1", "url2", "url3"]; // Replace with your file URLs const uploadedFiles = []; for (const url of fileUrls) { const response = await axios(this.$, { method: "post", url: "https://www.googleapis.com/upload/drive/v3/files", headers: { Authorization: `Bearer ${steps.google_drive.$auth.oauth_access_token}`, "Content-Type": "application/json", }, params: { uploadType: "media", }, data: { name: url.split("/").pop(), mimeType: "application/octet-stream", }, media: { mimeType: "application/octet-stream", body: (await axios(this.$, { url, responseType: "stream" })).data, }, }); uploadedFiles.push(response.data); } return uploadedFiles; }, }; Make sure to replace the fileUrls array with your actual file URLs and connect your Google Drive account in the steps.google_drive step when using this component in your workflow.

I get an error on auth :disappointed:

What do I do wrong?

Cannot read property ā€˜$authā€™ of undefined

I apologize for the confusion in my previous response. It seems there was an issue with the code I provided. Hereā€™s the corrected code snippet to upload multiple files to Google Drive using a single code step: import { axios } from "@pipedream/platform"; export default { name: "Upload Multiple Files to Google Drive", props: { google: { type: "app", app: "google_drive", }, fileUrls: { type: "string[]", label: "File URLs", description: "Enter the URLs of the files you want to upload to Google Drive", }, }, async run() { const uploadedFiles = []; for (const url of this.fileUrls) { const fileMetadata = { name: url.split("/").pop(), mimeType: "application/octet-stream", }; const media = { mimeType: "application/octet-stream", body: (await axios(this.$, { url, responseType: "stream" })).data, }; const response = await axios(this.$, { method: "POST", url: "https://www.googleapis.com/upload/drive/v3/files", headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }, params: { uploadType: "multipart", }, data: JSON.stringify(fileMetadata), media, }); uploadedFiles.push(response.data); } return uploadedFiles; }, }; Make sure to connect your Google Drive account in the google prop when using this component in your workflow. If you still encounter any issues, please visit https://pipedream.com/support for more support options.

I get this warning and no upload:

Warning
ā€¢ Code was still running when the step ended. Make sure to await all Promises, or promisify callback functions. May be a false positive