Home > Backend Development > Golang > Upload files directly to Google Bucket using REST API

Upload files directly to Google Bucket using REST API

WBOY
Release: 2024-02-05 23:15:03
forward
543 people have browsed it

使用 REST API 将文件直接上传到 Google 存储桶

Question content

Following the go example, I used the following code to upload files to google bucket:

func uploadFile(bucket string, uploadFilePath string, destFilePath string) error {
    os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "./credential.json")
    ctx := context.Background()
    client, err := storage.NewClient(ctx)
    if err != nil {
        return fmt.Errorf("storage.NewClient: %v", err)
    }
    defer client.Close()

    // Open local file.
    f, err := os.Open(uploadFilePath)
    if err != nil {
        return fmt.Errorf("os.Open: %v", err)
    }
    defer f.Close()

    ctx, cancel := context.WithTimeout(ctx, time.Second*50)
    defer cancel()
    o := client.Bucket(bucket).Object(destFilePath)
    o = o.If(storage.Conditions{DoesNotExist: true})
    
    // Upload an object with storage.Writer.
    wc := o.NewWriter(ctx)
    if _, err = io.Copy(wc, f); err != nil {
        return fmt.Errorf("io.Copy: %v", err)
    }
    if err := wc.Close(); err != nil {
        return fmt.Errorf("Writer.Close: %v", err)
    }
    return nil
}

func main() {
    bucket := "g1-mybucket-001"
    targetFilePath := "./somefile.txt"
    destFilePath := "it_poc_test_folder/somefile.txt"
    err := uploadFile(bucket, targetFilePath, destFilePath)
    if err != nil {
        fmt.Println(fmt.Errorf("Error uplolading file: %v", err))
    } else {
        fmt.Printf("%s uploaded to %s.\n", targetFilePath, destFilePath)
    }
}
Copy after login

Now I plan to wrap it up and create a rest api for file upload. Then I realized that the code is only for uploading local files. How do I wrap it so that the file goes directly into the bucket without uploading it to the server first?


Correct answer


There are many files to uploadExample, the main points are:

func uploadfile(w http.responsewriter, r *http.request) {

    // ... 
    r.parsemultipartform(10 << 20)
    f, handler, err := r.formfile("myfile")
    if err != nil {
        // handle err
    }
    defer f.close()
    // ...
}
Copy after login

So just pass the f body of the file upload to your cloud upload logic as if it were a local file:

// same code - different `f` source

// Upload an object with storage.Writer.
wc := o.NewWriter(ctx)
if _, err = io.Copy(wc, f); err != nil {
    return fmt.Errorf("io.Copy: %v", err)
}
Copy after login

The above is the detailed content of Upload files directly to Google Bucket using REST API. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
lol
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template