Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PUT uploads to object storage client #25

Merged
merged 1 commit into from
Jan 28, 2024
Merged

Conversation

br3ndonland
Copy link
Owner

@br3ndonland br3ndonland commented Jan 28, 2024

Description

Dotenv files are commonly kept in cloud object storage. fastenv provides an object storage client for downloading and uploading dotenv files.

S3-compatible object storage allows uploads with either POST or PUT. This PR will implement uploads with PUT.

Changes

This PR will add a method argument to fastenv.ObjectStorageClient.upload() that accepts either POST or PUT. POST was previously the default, but PUT will now be the default because PUT uploads are more widely-supported and standardized. Backblaze B2 does not currently support single-part uploads with POST to their S3 API (the B2 native API must be used instead), and Cloudflare R2 does not support uploads with POST at all.

Files will be opened in binary mode and attached with the content argument (httpx_client.put(content=content)) as suggested in the HTTPX docs.

Unlike downloads with GET, presigned PUT URL query parameters do not necessarily contain all the required information. Additional information may need to be supplied in request headers. In addition to supplying header keys and values with HTTP requests, header keys should be signed into the URL in the X-Amz-SignedHeaders query string parameter (alphabetically-sorted, semicolon-separated, lowercased).

These request headers can specify:

  • Object encryption. Encryption information can be specified with headers including X-Amz-Server-Side-Encryption. Note that, although similar headers like X-Amz-Algorithm are included as query string parameters in presigned URLs, X-Amz-Server-Side-Encryption is not. If X-Amz-Server-Side-Encryption is included in query string parameters, it may be silently ignored by the object storage platform. AWS S3 now automatically encrypts all objects and Cloudflare R2 does also, but Backblaze B2 will only automatically encrypt objects if the bucket has default encryption enabled.
  • Object integrity checks. The Content-MD5 header, defined by RFC 1864, can supply a base64-encoded MD5 checksum. After the upload is completed, the object storage platform server will calculate a checksum for the object in the same manner. If the client and server checksums are the same, this means that all expected information was successfully sent to the server. If the checksums are different, this may mean that object information was lost in transit, and an error will be reported. Note that, although Backblaze B2 accepts and processes the Content-MD5 header, it will report a SHA1 checksum to align with uploads to the B2-native API.
  • Object metadata. Headers like Content-Disposition, Content-Length, and Content-Type can be supplied in request headers.

Related

Copy link

vercel bot commented Jan 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
fastenv ✅ Ready (Inspect) Visit Preview Jan 28, 2024 9:56pm

@br3ndonland br3ndonland merged commit 6968353 into develop Jan 28, 2024
14 checks passed
@br3ndonland br3ndonland deleted the put-object branch January 28, 2024 22:13
br3ndonland added a commit that referenced this pull request Jan 29, 2024
Dotenv files are commonly kept in cloud object storage. fastenv provides
an object storage client for downloading and uploading dotenv files.
https://fastenv.bws.bio/cloud-object-storage

The fastenv object storage client currently supports AWS S3 and
Backblaze B2. There are many other object storage platforms with
"S3-compatible" APIs, including Cloudflare R2. This commit will
implement support for Cloudflare R2.
https://developers.cloudflare.com/r2/

- Handle Cloudflare R2 bucket hosts
  - The fastenv object storage client works with "virtual-hosted-style"
    URLs, as this is the preferred format for AWS S3
  - Cloudflare R2 "virtual-hosted-style" URLs, like
    `https://<BUCKET>.<ACCOUNT_ID>.r2.cloudflarestorage.com`, were
    implemented on 2022-05-16.
- Handle Cloudflare R2 bucket region `auto`
  https://developers.cloudflare.com/r2/api/s3/api/
- Add Cloudflare R2 credentials to tests
- Skip tests that do uploads with `POST` (Cloudflare R2 only supports
  uploads with `PUT`
  https://developers.cloudflare.com/r2/api/s3/presigned-urls/
- Update object storage docs with info on Cloudflare R2

#8
#25
[Cloudflare R2](https://developers.cloudflare.com/r2/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant