# Upload a file to google drive

**URL:** <https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [February 16, 2023, 6:01pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954 "2023-02-16T18:01:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bastian\_Ignacio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bastian_ignacio/32/43245_2.png) [@Bastian\_Ignacio](https://forum.inductiveautomation.com/u/Bastian_Ignacio)\
**Post date:** [February 16, 2023, 6:01pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/1 "2023-02-16T18:01:05Z")

</div>

Hello everyone!

I have a code that works for me in vs code, but the request and json doesn't work directly in ignition:

```auto
#that code works for me on vs code
import json
import requests
headers = {
    "Authorization":"Bearer xxxx-xxxxx"
}

para = {
    "name":"ejemplo3.jpg",
    "parents":["xxxxxx"]
}

files = {
    'data':('metadata',json.dumps(para),'application/json;charset=UTF-8'),
    'file':open('Screenshot_2.png','rb')
}

r = requests.post("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers=headers,
    files=files
)

```

I am looking for a way to select a file with file explorer, and upload to google drive, that is why I have the first part of the code where I select the file and get the name:

```auto
import os

#paso 1
selectedFile = event.source.parent.getComponent('file_explorer').selectedPath
print "archivo seleccionado:"
print selectedFile
flag_src_path_IsFile = event.source.parent.getComponent('file_explorer').selectedPathIsFile
print "flag_src_path_IsFile:"
print flag_src_path_IsFile

nameFile = os.path.basename(selectedFile)
print "nombre el archivo seleccionado en file explorer:"
print nameFile

```

and the step 2 that I have to use the google playground api is this, although it doesn't work for me ☹

it could be something like this:

```auto
import os
import urllib2
import json

# Token de acceso generado en Google Cloud Console
access_token = "xxxxxxxxxxxxxx"

# URL de la API de Google Drive para subir archivos
url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

# Encabezados de la solicitud, incluyendo el token de acceso
headers = {
    "Authorization": "Bearer " + access_token,
    "Content-Type": "multipart/related; boundary=foo"
}

# Metadatos del archivo, incluyendo el nombre y la ubicación
metadata = {
    "name": "energia-solar.jpg",
    "parents": ["xxxxxx"]
}

# Contenido del archivo, codificado en base64
file_path = r"C:\Users\BJ_IT\Downloads\aimage.png"
with open(file_path, "rb") as f:
    file_content = f.read()
file_content_base64 = file_content.encode("base64").replace("\n", "")

# Cuerpo de la solicitud
body = (
    "--foo\r\n" +
    "Content-Type: application/json; charset=UTF-8\r\n\r\n" +
    json.dumps(metadata) + "\r\n" +
    "--foo\r\n" +
    "Content-Type: image/jpeg\r\n" +
    "Content-Transfer-Encoding: base64\r\n" +
    "Content-Disposition: attachment; filename=\"energia-solar.jpg\"\r\n\r\n" +
    file_content_base64 + "\r\n" +
    "--foo--"
)

# Crear y enviar la solicitud
request = urllib2.Request(url, data=body, headers=headers)
response = urllib2.urlopen(request,timeout=10)

# Leer la respuesta y obtener el ID del archivo subido
response_data = json.loads(response.read())
file_id = response_data["id"]

print("Archivo subido con éxito. ID del archivo: " + file_id)

```

could i get help? I have read:

> [@HTTP GET request scripting](https://forum.inductiveautomation.com/t/http-get-request-scripting/12324/2):
>
> Solved using the following code: [code]import system import urllib2 Generate URL for API call to read products url = “[https://inventory.dearsystems.com/ExternalApi/Products?Page=1&Limit=100&Name=Log](https://inventory.dearsystems.com/ExternalApi/Products?Page=1&Limit=100&Name=Log)” Create header with information required by API urlHeader = {“Content-Type”: “application/json”, “api-auth-accountid”: “XXXXX”, “api-auth-applicationkey”: “XXXXX”} request = urllib2.Request(url, headers=urlHeader) contents = urllib2.urlopen(request).read() data = system.util.jsonDecode(content…

> [@File Upload component](https://forum.inductiveautomation.com/t/file-upload-component/59117/2):
>
> in theory you can In the event onFileReceived, use the method event.file.getBytes() Then sending it to google drive, but you will need to check the authentication process: [Upload file data &nbsp;|&nbsp; Drive API &nbsp;|&nbsp; Google Developers](https://developers.google.com/drive/api/guides/manage-uploads#http)

I appreciate your answers...

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 16, 2023, 7:16pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/2 "2023-02-16T19:16:02Z")

</div>

If this end point works to upload a file in general using Postman or similar testing, I would suspect the issues lay in the jython libraries. Don't use requests/urllib, use system.net.httpClient. Try re-writing your script using the functions here - [system.net.httpClient - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.net.httpClient)

---

<div class="post-metadata">

**Author:** ![calimero100582](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/calimero100582/32/18630_2.png) [@calimero100582](https://forum.inductiveautomation.com/u/calimero100582)\
**Post date:** [February 16, 2023, 7:48pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/3 "2023-02-16T19:48:40Z")

</div>

on my side, I am not sure I understand this:

```python
file_content.encode("base64").replace("\n", "")

```

Is it really working to encode base64? also why the replace? base64 should not contains new line

I would do the following

```python
from com.inductiveautomation.ignition.common import Base64
file_content_base64 = Base64.encode(file_content)

```

---

<div class="post-metadata">

**Author:** ![Bastian\_Ignacio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bastian_ignacio/32/43245_2.png) [@Bastian\_Ignacio](https://forum.inductiveautomation.com/u/Bastian_Ignacio)\
**Post date:** [February 16, 2023, 7:50pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/4 "2023-02-16T19:50:38Z")

</div>

With this code it seems I'm very close...

```auto
import json
import system.net as net

url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
headers = {"Authorization": "Bearer xxxxxxxxxxx"}

params = {"name": "energia-solar.jpg", "parents": ["xxxxxxxxxxxxxxx"]}
metadata = json.dumps(params)

files = {
    "data": ("metadata", metadata, "application/json;charset=UTF-8"),
    "file": ("energia-solar.jpg", system.file.readFileAsBytes("C:\\Users\\BJ_IT\\Downloads\\energia-solar.jpg"), "image/jpeg"),
}

client = net.httpClient()
response = client.post(url, headers=headers, data=files)

if response.good:
    print(response.json)
else:
	print "error"
	print (response.json)

```

and the console give me:

```auto
{'kind': u'drive#file', 'mimeType': u'application/json', 'name': u'Untitled', 'id': u'12DPzTFb-fA9UIAMG9p2CW7pcAonBUIbw'}

```

So I understand that you are not receiving a file, it is empty. so I should read as bytes, probably with something similar to this...

```auto
# Abre el archivo y léelo en una variable como un objeto de bytes
with open('C:\Users\BJ_IT\Downloads\energia-solar.jpg', 'rb') as f:
    file_data = f.read()

```

I'll keep trying and comment..

---

<div class="post-metadata">

**Author:** ![Bastian\_Ignacio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bastian_ignacio/32/43245_2.png) [@Bastian\_Ignacio](https://forum.inductiveautomation.com/u/Bastian_Ignacio)\
**Post date:** [February 16, 2023, 7:57pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/5 "2023-02-16T19:57:43Z")

</div>

type object 'com.inductiveautomation.ignition.common.Base64' has no attribute 'encode'

I'll keep trying something similar to the last code posted here!  
I appreciate your answer

---

<div class="post-metadata">

**Author:** ![calimero100582](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/calimero100582/32/18630_2.png) [@calimero100582](https://forum.inductiveautomation.com/u/calimero100582)\
**Post date:** [February 16, 2023, 8:10pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/6 "2023-02-16T20:10:51Z")

</div>

have you tried simple upload instead of multipart: [Fazer upload dos dados do arquivo &nbsp;|&nbsp; Google Drive &nbsp;|&nbsp; Google Developers](https://developers.google.com/drive/api/guides/manage-uploads#simple)

sorry it's encodeBytes ([Base64](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.25/com/inductiveautomation/ignition/common/Base64.html#encodeBytes(byte%5B%5D,int)))

```auto
file_content_base64 = Base64.encodeBytes(file_content)

```

---

<div class="post-metadata">

**Author:** ![Bastian\_Ignacio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bastian_ignacio/32/43245_2.png) [@Bastian\_Ignacio](https://forum.inductiveautomation.com/u/Bastian_Ignacio)\
**Post date:** [February 16, 2023, 9:00pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/7 "2023-02-16T21:00:47Z")

</div>

> [@calimero100582](#):
>
> have you tried simple upload instead of multipart: [Fazer upload dos dados do arquivo | Google Drive | Google Developers](https://developers.google.com/drive/api/guides/manage-uploads#simple)

I tried the first code on vs code and works fine

Although I still can't find the solution, which apparently is reading the file in bytes

---

<div class="post-metadata">

**Author:** ![calimero100582](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/calimero100582/32/18630_2.png) [@calimero100582](https://forum.inductiveautomation.com/u/calimero100582)\
**Post date:** [February 17, 2023, 3:21pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/8 "2023-02-17T15:21:17Z")

</div>

Where do you run your code? Vision or Gateway?

---

<div class="post-metadata">

**Author:** ![Bastian\_Ignacio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bastian_ignacio/32/43245_2.png) [@Bastian\_Ignacio](https://forum.inductiveautomation.com/u/Bastian_Ignacio)\
**Post date:** [February 17, 2023, 4:56pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/9 "2023-02-17T16:56:22Z")

</div>

Ignition designer

I still do not get satisfactory results, I even added oauth2 and google-api-python libraries, as I am also looking at the google console (GCP) in case something works correctly

this is my latest code

```auto
import json
import net
import system
import os
import system.net as net

token_acceso = event.source.parent.getComponent('txt_token').text
carpeta = event.source.parent.getComponent('txt_carpeta').text

#url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media"
#url = "https://www.googleapis.com/upload/drive/v3/files"

headers = {"Authorization": "Bearer "+token_acceso}

selected_file = system.file.openFile("pdf")
#selected_file = system.file.openFile("")
if selected_file != None:
    # Obtenemos el nombre del archivo seleccionado
    file_name = os.path.basename(selected_file)
    
    print "nombre archivo: "+file_name

    # Leemos el contenido del archivo seleccionado
    file_content = system.file.readFileAsBytes(selected_file)

    params = {"name": file_name, "parents": [carpeta]}
    metadata = json.dumps(params)

    files = {
    "data": ("metadata", metadata, "application/json;charset=UTF-8"),
    "file": (file_name, file_content, "application/pdf")
    }

    client = net.httpClient()
    response = client.post(url, headers=headers, data=files)

    if response.good:
        print(response.json)
    else:
      # print("Error: " + response.json)
      print ("error: " + response)

```

I already have about 10 buttons with different codes in the designer, ups

and sadly none work (yet)

---

<div class="post-metadata">

**Author:** ![calimero100582](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/calimero100582/32/18630_2.png) [@calimero100582](https://forum.inductiveautomation.com/u/calimero100582)\
**Post date:** [February 17, 2023, 5:07pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/10 "2023-02-17T17:07:30Z")

</div>

the code is executed on the gateway, so the file must be on the gateway

---

<div class="post-metadata">

**Author:** ![Bastian\_Ignacio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bastian_ignacio/32/43245_2.png) [@Bastian\_Ignacio](https://forum.inductiveautomation.com/u/Bastian_Ignacio)\
**Post date:** [February 17, 2023, 6:31pm UTC](https://forum.inductiveautomation.com/t/upload-a-file-to-google-drive/69954/11 "2023-02-17T18:31:55Z")

</div>

In mi g. drive root folder ive multiple files called "Untiled"

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/1/e191a9bdc6776e956ad8f8deaf609e5e1d15afae.png)

when i open any, surprise..:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/7/7752cc158f425d514e9e3358fd18d3e257511604.png)

Almost...
