Upload and download objects
Upload objects to an Object Storage bucket from Azion Console, the API, or the Azion CLI, then list, download, replace, and delete them.
You upload, list, download, replace, and delete the objects of an Object Storage bucket from Azion Console, the Azion API, the Azion CLI, or a function. An object exists as soon as its upload succeeds, stored under the key you gave it. The bucket then lists that key with its size and the time it was last modified.
To create the bucket itself, or to change the access level it carries, refer to Create a bucket.
Prerequisites
- A bucket. To create one, refer to Create a bucket.
- A file on your machine to upload.
- Access to Azion Console, for the Console procedures. Refer to Access Azion Console.
- A personal token, for the API procedures.
- The Azion CLI installed and authorized, for the CLI procedures.
Upload an object using Azion Console
Azion Console is the shortest path for a handful of files. The API and the Azion CLI put the same upload inside a script or a pipeline. The files land in the files area of the bucket, described as “Browse, upload, and manage objects stored in this bucket.” To upload them:
Access Azion Console > Object Storage > Buckets, then select the bucket.
Select Upload files for one or more files, or Upload folder for a directory. Dragging the files onto the drop target, which reads “Drag files here to add them to your bucket”, adds them as well.
Each file is stored as an object of the bucket and appears in the files area under its key.
Upload an object using the API
The bucket name and the object key travel in the path, and the file travels in the request body. To upload the object:
Replace [TOKEN VALUE] with your personal token, my-bucket with your bucket, and folder/file.csv with the key you want:
The API answers with HTTP 201, or 202 when it processes the request asynchronously, and names the key it stored the object under:
The object is stored under folder/file.csv. The folder/ segment is part of the key: Object Storage creates the prefix with the upload, and no request creates it beforehand.
Upload an object using the Azion CLI
The command reads the file named in --source and stores it under --object-key. To upload the object:
The command prints one line:
The object is stored in the bucket, under the key you passed in --object-key.
List the objects in a bucket
Every interface returns the keys the bucket holds, and the API narrows the list to one prefix.
Azion Console
Access Azion Console > Object Storage > Buckets, then select the bucket. Its files area lists the objects it holds.
The API
Send a GET request to the objects endpoint:
The response carries one entry per object: its key, the time it was last modified, its size in bytes, and whether the entry is a prefix.
Four query parameters shape the listing:
| Parameter | What it does |
|---|---|
prefix | Returns the keys that begin with the value. The default is empty, which returns every key |
all_levels | Defaults to true and returns the keys at every level under the prefix |
max_object_count | Sets how many entries one response carries, up to 1,000 |
continuation_token | Returns the entries that follow the token the previous response carried |
With all_levels=false, a prefix is returned as one entry instead of the keys under it:
The Azion CLI
Name the bucket with --bucket-name:
The command prints a table with a KEY column and a LAST MODIFIED column. --details adds a SIZE column, and --next-page moves to the next page.
The listing names every key the bucket holds, which is how you confirm an upload arrived.
Download an object
A download returns the bytes of one object, addressed by its key.
The API
Send a GET request to the key:
The API answers with HTTP 200 and the object as application/octet-stream, so the terminal prints the contents of the object. A key that does not exist answers with HTTP 404 and error 17013, Object Does Not Exist.
To write the object to a local file instead, add the curl options -O and -J. They take the object name from the response headers and write the output into a file:
The Azion CLI
Name the bucket and the key:
The command prints the content of the object.
You now hold the object’s bytes, in the terminal or in a local file.
Replace an object
Two methods write over the object stored under a key, and they differ in what they do with a key that does not exist yet. POST stores the object either way and answers with HTTP 201. PUT replaces only: a PUT to a key that does not exist answers with HTTP 404 and error 17013, Object Does Not Exist.
The API
Send a PUT request to the key, with the new file as the body:
The API answers with HTTP 200, or 202 when it processes the request asynchronously, and names the key it replaced:
The Azion CLI
Name the new file in --source:
The command prints one line:
azion update storage object -h lists every flag it accepts.
The key now addresses the new content, and the content it held is gone.
Delete an object
A delete is asynchronous. Azion accepts the request, the key leaves the listing at once, and the object is permanently removed after a 24-hour grace period.
Azion Console
To delete the objects:
Access Azion Console > Object Storage > Buckets, then select the bucket.
Azion Console asks “Are you sure you want to delete the selected files?” before it removes them.
The objects leave the files area of the bucket.
The API
Send a DELETE request to the key:
The API answers with HTTP 202 and reports the delete as pending:
The key is gone from the listing, and a request for it answers with HTTP 404 right away.
The Azion CLI
Name the bucket and the key:
The command names the key it removed:
The object answers no request, and a second delete of the same key returns error 17013, Object Does Not Exist.
Read and write objects from a function
A function reaches the same bucket through the azion:storage module, so an application stores what a request carries and returns what the bucket holds. To put the bucket behind a function:
Create a function in Functions with this code. It routes by method. A POST writes the request body under the key taken from the request path. A GET reads the object back and answers with the content type it was stored with.
The values it carries:
| Variable | Description |
|---|---|
path | The path to the object. Example: ./path/file.csv |
bucket_name | The name of the bucket. Example: my-bucket |
content_type | The MIME type of the object. Example: text/csv |
value | The contents of the object, as binary data |
The function reads the bucket name from its own arguments, so add the bucket property with the name of your bucket as a string:
A function answers a request only once an application runs it, so instantiate the function in the application that receives the uploads.
A POST to the application stores the request body in the bucket. A GET to the same path returns the object with the content type it was stored with.