Open In App

How to do Cloud File Sharing using Python?

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to share files online using a free, secure platform called GoFile. We can make it available for anyone through the link generated with the help of Python. Gofile is a platform for sharing and storing files. You have no restrictions on how much content you can save or share. Gofile can accommodate your needs whether you want to save your data in the file manager, share your files to pals, or broadcast your material to thousands of people.

Cloud File Sharing using Python

Gofile

Gofile is a free, anonymous file-sharing platform that enables users to upload and share files without sign-ups. It provides user privacy and security by offering many features like anonymous uploads, file deletion, limited lifespan, and no tracking facility. Users can upload various formats and generate shareable links to share the files.

To install the Gofile module, run the following command in the Terminal Window:

pip install gofile

Working Procedure of GoFile:

  • Using accounts, files, and folders, the Gofile system controls a productive file storage system. Its API enables effective file management and organization. A root folder exists for each account and cannot be removed.
  • A guest account and root folder are formed when a file is uploaded without any parameters being specified, and the file is then uploaded to a new folder inside the root folder.
  • Upload the first file, get the folderId from the response, and then upload each further file one at a time while including the folderId as a parameter.

API:

API is an abbreviation for “Application Programming Interface.” Different software applications can connect and interact with one another through a set of rules, protocols, and tools. Now we can see the API’s used:

GET https://api.gofile.io/getServer

  • Returns the best server available to receive files.

Curl example:

curl https://api.gofile.io/getServer

JavaScript example:

Javascript

fetch('https://api.gofile.io/getServer')
  .then(response => response.json())
  .then(data => {
    if (data.status === 'ok') {
      console.log(data.data.server)
    }
  })
  .catch(error => console.error(error))

Response Example:

Javascript

{
  "status": "ok",
  "data": {
    "server": "store1"
  }
}

POST https://(server}.gofile.io/uploadFile

  • Upload one file on a specific server.
  • The file will be added to the folder with the specified folderId.

Parameters:

  • File (required) :
  1. Must contain one file.
  2. If you want to upload more than one file, use uploadFile once more and include the folderId of the first file you uploaded.
  • Token (optional) :
  1. The access token of an account. Can be retrieved from the profile page.
  2. If valid, the file will be added to this account.
  3. If undefined, a guest account will be created to receive the file.
  • FolderId (optional) :
  1. The ID of a folder.
  2. If valid, the file will be added to this folder.
  3. If undefined, a new folder will be created to receive the file.
  4. When using the folderId, you must pass the account token.

Curl example:

curl -F file=@someFile.txt https://store1.gofile.io/uploadFile

JavaScript example:

Javascript

const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('https://store1.gofile.io/uploadFile', {
    method: 'POST',
    body: formData,
  })
  .then(response => response.json())
  .then(data => {
    if (data.status === 'ok') {
      console.log(data.data)
    }
  })
  .catch(error => console.error(error))

Response Example:

Javascript

{
  "status": "ok",
  "data": {
    "downloadPage": "https://gofile.io/d/Z19n9a",
    "code": "Z19n9a",
    "parentFolder": "3dbc2f87-4c1e-4a81-badc-af004e61a5b4",
    "fileId": "4991e6d7-5217-46ae-af3d-c9174adae924",
    "fileName": "example.mp4",
    "md5": "10c918b1d01aea85864ee65d9e0c2305"
  }
}

Code:

Python3

# Import the 'gofile' module as 'go'
import gofile as go

# Define a function 'Store_Files' that takes a 'file' as input


def Store_Files(file):
    # Get the current server for file upload using 'getServer()' function from 'gofile' module
    cur_server = go.getServer()
    # Print the current server to the console
    print(cur_server)

    # Upload the file using 'uploadFile()' function from 'gofile' module
    # 'file' is the path to the file you want to upload
    url = go.uploadFile(file)

    # Print the download link to the uploaded file
    print("Download Link: ", url["downloadPage"])


# Call the 'Store_Files' function and pass the path to the file you want to upload
Store_Files(r"C:\Users\DELL\Desktop\INTERNSHIP\Bg (1).jpg")
# Contributed by PL VISHNUPPRIYAN

Working of the code:

Input File:

Input: The file that is uploaded to the Cloud Storage

Output:

Output: in Terminal Window
Output: The uploaded file is visible in the Cloud Storage


Like Article
Suggest improvement
Next
Share your thoughts in the comments

Similar Reads

File Sharing App using Python
Additive Secret Sharing and Share Proactivization - Using Python
Google Cloud Platform - Running Different Versions of Python on Google Cloud Run
Implementing Shamir's Secret Sharing Scheme in Python
Sharing Notebook in Google Colab
reStructuredText | .rst file to HTML file using Python for Documentations
Create a GUI to convert CSV file into excel file using Python
How to save file with file name from user using Python?
How to convert PDF file to Excel file using Python?
How to convert CSV File to PDF File using Python?
Practice Tags :