Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
awmpietro committed Oct 5, 2023
0 parents commit f91b40c
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below if you want to check in your vendored code)
#vendor/

# IDE and editor files
*.idea/
*.vscode/
*.suo
*.ntvs*
*.njsproj
*.sln
*.swp

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

57 changes: 57 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# gcpfileupload

gcpfileupload is a Go package that simplifies the process of uploading a file to a bucket in Google Cloud Storage.

## Installation

To use this package, install it using go get:

```bash
go get github.com/awmpietro/gcpfileupload
```

## Usage:

First, import the package:

```go
import "github.com/awmpietro/gcpfileupload"
```

Create an instance of FileUploader:

```go
fileUpload := gcpfileupload.FileUploader{
Request: r,
FormFile: "file",
FileName: "my-new-name",
BucketName: "your-google-cloud-storage-bucket-name",
}
```

Call the Upload function:

```go
hasFile, err := fileUpload.Upload()
```

The Upload function returns:

A boolean hasFile: true if there is a file to upload.
false if not.

An error:
Returns nil if there is no error.
Returns the error encountered during the upload process otherwise.

##Configuration
Make sure to set up the GOOGLE_STORAGE_BUCKET_NAME environment variable in your environment. This should point to the path of the Google user account key you've previously created:

```yaml
export GOOGLE_STORAGE_BUCKET_NAME=path_to_your_google_account_key.json
```

Replace path_to_your_google_account_key.json with the actual path to your Google user account key file.

##Contributing
Feel free to open issues or PRs if you find any problems or have suggestions!
61 changes: 61 additions & 0 deletions gcpfileupload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gcpfileupload

import (
"context"
"io"
"net/http"
"path/filepath"
"time"

"cloud.google.com/go/storage"
)

type FileUploader struct {
Request *http.Request
FormFile string
FileName string
BucketName string
}

// Upload a file to a bucket in Google Cloud Storage
// Make sure you have defined the env var GOOGLE_STORAGE_BUCKET_NAME in your env file
// The env should point to the path of your google user account keys
func (fu *FileUploader) Upload() (bool, error) {
file, header, err := fu.Request.FormFile(fu.FormFile)
if err != nil {
return false, err
}
defer file.Close()
if file != nil {

if fu.FileName == "" {
fu.FileName = header.Filename
} else {
ext := filepath.Ext(header.Filename)
fu.FileName = fu.FileName + ext
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()

client, err := storage.NewClient(ctx)
if err != nil {

return false, err
}
bucket := client.Bucket(fu.BucketName)
obj := bucket.Object(fu.FileName)
wc := obj.NewWriter(ctx)
if _, err := io.Copy(wc, file); err != nil {

return false, err
}
if err := wc.Close(); err != nil {
return false, err
}
return true, nil
}
return false, nil

}
33 changes: 33 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module github.com/awmpietro/gcpfileupload

go 1.20

require cloud.google.com/go/storage v1.33.0

require (
cloud.google.com/go v0.110.4 // indirect
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.132.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
Loading

0 comments on commit f91b40c

Please sign in to comment.