-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
359f473
commit 1643bdc
Showing
2 changed files
with
49 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Description: This file contains the functions to interact with Google Cloud Storage. | ||
package cmd | ||
|
||
import ( | ||
"cloud.google.com/go/storage" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// deleteFile deletes a file from Google Cloud Storage | ||
func deleteFile(client *storage.Client, bucketName, objectName string) error { | ||
bucket := client.Bucket(bucketName) | ||
object := bucket.Object(objectName) | ||
|
||
return object.Delete(context.Background()) | ||
} | ||
|
||
// uploadFile uploads a file to Google Cloud Storage | ||
// document this function with comments | ||
func uploadFile(client *storage.Client, bucketName, objectName, filePath string) (string, error) { | ||
bucket := client.Bucket(bucketName) | ||
object := bucket.Object(objectName) | ||
newCtx := context.Background() | ||
wc := object.NewWriter(newCtx) | ||
|
||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to open file: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
if _, err := io.Copy(wc, file); err != nil { | ||
return "", fmt.Errorf("failed to upload file: %w", err) | ||
} | ||
if err := wc.Close(); err != nil { | ||
return "", fmt.Errorf("failed to close writer: %w", err) | ||
} | ||
|
||
// Return the public URL of the uploaded object. | ||
publicURL := fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucketName, objectName) | ||
|
||
fmt.Printf("File %s uploaded to bucket %s as %s and is available at %s.\n", filePath, bucketName, objectName, publicURL) | ||
return publicURL, nil | ||
} |