Linux

How to Upload Files Using cURL

In this tutorial, we are going to show you how you can use cURL to upload files to various endpoints such as API, AWS S3, Google Storage Bucket, and more.
Captain Salem 4 min read
How to Upload Files Using cURL

Anyone who has ever used a terminal on Unix-like systems, or even Windows based systems is familiar with cURL. cURL, short for Client URL is a powerful and very useful command-line utility and library for transferring data using various network protocls.

cURL supports a plethora of network protocols such as HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP, FILE, among others.

Whether you want to download files, upload file, send API requests, etc from the command-line, cURL is probably one of the most powerful and efficient tool to use.

A common task when working in the terminal is uploading files using various protocols such as FTP, etc.

cURL Upload File General Syntax

To upload a file with cURL, you can use HTTP POST method followed by the -F otopn as shown in the syntax below:

curl -F "<field=@path_to_local_File>"<url>

In the syntax above:

  1. field - this specifies the name of the form field that will recieve the file.
  2. path_to_local_file - specifies the path to the local file we wish to upload.
  3. url - specifies the URL where we want to upload the file.

An example is as shown:

curl -F "file=@sample.txt" https://example.com/upload

The command above will send an HTTP POST request to the specified URL with the form field named file and containing the contents of the PDF file.

NOTE; This technique only applies when uploading files via HTTP or HTTPS protocols. This cannot work with protocols such as FTP or SFTP.

Upload Using HTTP PUT

You are probably familiar with the HTTP PUT method. We can also use it to upload a file using cURL by the use of the -T flag. The command syntax is as shown:

curl -T <path_to_local_file> <url>

Where:

  • path_to_local_file - specifies path to the local file we wish to upload.
  • url - specifies the URL where we wisht o send the file.

An example is as shown:

curl -T ./sample.txt https://example.com/upload

The command above should send an HTTP PUT request to the specified URL with the specified file as the message body.

Upload FIle to FTP Server

Mainly when we are dealing with files, we are working with a dedicated FTP server hosted somewhere. Luckily in cURL, we can use the command syntax below to upload file to an FTP server.

curl -T <path_to_local_file> -u <username>:<password>ftp://<ftp_server_url>/<remote_directory>/

In the above syntax:

  • path_to_local_file- specifies the path to the local file we wish to upload.
  • username - sets the username for the FTP server.
  • password - specifies the associated password for the target username.
  • ftp_server_url- sets teh URL to the FTP server to which you wish to connect.
  • remote_directory - specifies the directory on the FTP server where the file will be uploaded.

An example is as shown:

curl -T ./sample.txt -u admin:password ftp://ftp.example.com/uploads/

This should connect to the specified FTP server and upload the file. It is good to ensure that you provide the correct credentials to the server and have write permissions on the specified directory,

Upload File SFTP Server

SFTP or SSH File Transfer Protocol or Secure File Protocol is a file tranfer and management protocol over secure and reliable data stream using SSH. This ensures that the commands and credentials are encrypted which makes it much secure than regular FTP.

In cURL, we can upload a file to SFTP server using the command syntax below:

curl -T <path_to_local_file> sftp://<username>@<sftp_server_url>/<remote_directory>/

In the command syntax above:

  • path_to_local file - this sets the path to the local file we wish to upload.
  • username - specifies the username to the SFTP server.
  • sftp_server_url - defines the URL of the SFTP server to which we wish to connect.
  • remote_directory - sets the remote directory on the SFTP server to which we wish to upload the file.

An example is as shown:

curl -T ~/sample.txt sftp://admin@sftp.example.com/uploads/

Upload to Amazon S3 Bucket (Pre-Signed)

Amazon S3 provides us with a feature called pre-signed URL which is a way of generating a temporary access to an object in the S3 bucket.

We can use presigned URL to upload file via cURL as shown:

curl -X PUT -F sample.txt  https://pre-signed-url

This should upload the specified file to the specified URL using your AWS S3 creds.

Upload File to Artifactory

We can also use cURL to upload files such as binaries to Artifactory using the command as shown:

$ curl -u <username>:<password> -T "<path_to_local_file>" "<artifactory_url>/artifactory/<repository>/<target_path>/<filename>"

In this syntax:

  • username - sets the Artifactory username
  • password - password associated with your Artifactory username.
  • path_to_local_file - specifies path to the local file we wish to upload.
  • artifactory_url - specifies the URL to target Artifactory instance.
  • repository - defines the Artifactory repository where to store the file.
  • target_path - this is an optional directory path within the specified repository on where to store the file.
  • filename - specifies the name we wish to assign the uploaded file.

This should allow you to upload a specified file to an Artifactory instance.

Upload File to Google Storage Bucket

To upload a file to a Google Cloud Storage bucket using the cURL command, we can use an authenticated POST request with the appropriate headers for authorization.

Before that, we need to setup an OAuth 2.0 token for authentication. This requires setting up a Google Cloud project, enabling the Google Cloud Storage API, and obtaining credentials.

Once configured, we can run the command as shown below to upload the file:

curl -X POST --data-binary @local-file-path \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: file-content-type" \
  "https://storage.googleapis.com/upload/storage/v1/b/YOUR_BUCKET_NAME/o?uploadType=media&name=remote-file-name"

Where:

  • local-file-path - specifies the path to the file we wish to upload.
  • YOUR_ACCESS_TOKEN - sets the OAuth 2.0 access token created earlier.
  • file-content-type - this defines the MIME type of the file we are uploading, such as image/jpeg for JPEG images, etc.
  • YOUR_BUCKET_NAME - defines the name of the Google Cloud Storage bucket.
  • remote-file-name - sets the desired name of the file once it's uploaded to the bucket.

Conclusion

In this tutorial, we learned how we can use cURL to upload files to various protocols such as HTTP, HTTPS. FTP, SFTP, Amazon S3, Artifactory, and more.

:) Cheers!

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.