https://github.com/truesparrowsystems/go-presigned-post
https://github.com/truesparrowsystems/go-presigned-post
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/truesparrowsystems/go-presigned-post
- Owner: TrueSparrowSystems
- License: mit
- Created: 2024-01-25T06:49:02.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-02-22T06:36:54.000Z (about 1 year ago)
- Last Synced: 2025-01-02T08:19:35.413Z (4 months ago)
- Language: Go
- Size: 97.7 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AWS S3 Presigned POST in Go
The `go-presigned-post` package provides a convenient way to generate a presigned POST URL to securely upload files to Amazon S3 using HTTP POST requests.
## Installation
```bash
go get -u github.com/TrueSparrowSystems/go-presigned-post
```## Usage
```go
package mainimport (
"fmt"
"github.com/TrueSparrowSystems/go-presigned-post"
)func main() {
// Set your AWS credentials and configuration
awsCredentials := presignedpost.AwsCredentialsAndConfig{
Region: "your-aws-region",
Bucket: "your-s3-bucket",
AccessKeyID: "your-access-key-id",
SecretAccessKey: "your-secret-access-key",
}// Set the key for the S3 object
key := "path/to/upload/file.txt"// Set optional policy options
policyOptions := presignedpost.PolicyOptions{
ExpiryInSeconds: nil, // Default is 1 hour
ContentType: "", // Content type of the S3 object
MaxFileSizeInBytes: 0, // Maximum allowed file size in the policy
Acl: "", // AWS S3 ACL (Access Control List)
CacheControl: "", // Cache control header
}// Generate presigned POST URL and fields
postUrl, postFields, err := presignedpost.PresignedPostObject(key, awsCredentials, policyOptions)
if err != nil {
fmt.Println("Error:", err)
return
}// Use postUrl and postFields to upload a file using HTTP POST
// ...fmt.Println("Presigned POST URL:", postUrl)
fmt.Println("Presigned POST Fields:", postFields)
}
```## Documentation
### `PresignedPostObject`
```go
func PresignedPostObject(key string, awsCredentialsAndConfig presignedpost.AwsCredentialsAndConfig, policyOpts presignedpost.PolicyOptions) (string, presignedpost.PresignedPostRequestFields, error)
```Generates a presigned POST URL and fields for uploading a file to S3.
- `key`: Key (file path) for the S3 object.
- `awsCredentialsAndConfig`: AWS credentials and configuration.
- `policyOpts`: Policy options (expiration time, content type, etc.).### `AwsCredentialsAndConfig`
```go
type AwsCredentialsAndConfig struct {
Region string // AWS region.
Bucket string // AWS S3 bucket.
AccessKeyID string // AWS access key.
SecretAccessKey string // AWS secret access key.
}
```### `PolicyOptions`
```go
type PolicyOptions struct {
ExpiryInSeconds *int // Expiration time in seconds for the policy. Default is 3600.
ContentType string // Content type of the S3 object.
MaxFileSizeInBytes int // Maximum allowed file size in the policy.
Acl string // AWS S3 ACL (Access Control List). Default is private.
CacheControl string // Cache control header. Default is none.
}
```### `PresignedPostRequestFields`
```go
type PresignedPostRequestFields struct {
Key string `json:"key"` // S3 object key.
Bucket string `json:"bucket"` // S3 bucket.
XAmzAlgorithm string `json:"X-Amz-Algorithm"` // AWS algorithm header.
XAmzCredential string `json:"X-Amz-Credential"` // AWS credential header.
XAmzDate string `json:"X-Amz-Date"` // AWS date header.
Policy string `json:"Policy"` // Base64-encoded policy.
XAmzSignature string `json:"X-Amz-Signature"` // AWS signature header.
ContentType string `json:"Content-Type"` // Content type header.
CacheControl string `json:"Cache-Control"` // Cache control header.
Acl string `json:"acl"` // AWS S3 ACL header.
}
```## Contribution
We welcome more helping hands to make the package better. Feel free to report issues, and raise PRs for fixes & enhancements.
Built with :heart: by True Sparrow