dream2891 2014-03-12 12:37
浏览 78

使用软件包文件写入Cloud Storage?

Golang provides the file package to access Cloud Storage.

The package's Create function requires the io.WriteCloser interface. However, I have not found a single sample or documentation showing how to actually save a file to Cloud Storage.

Can anybody help? Is there a higher level implementation of io.WriteCloser that would allow us to store files in Cloud Storage? Any sample code?

We've obviously tried to Google it ourselves but found nothing and now hope for the community to help.

  • 写回答

1条回答 默认 最新

  • doushifen4060 2014-03-12 12:51
    关注

    It's perhaps true than the behavior is not well defined in the documentation.

    If you check the code: https://code.google.com/p/appengine-go/source/browse/appengine/file/write.go#133

    In each call to Write the data is sent to the cloud (line 139). So you don't need to save. (You should close the file when you're done, through.)

    Anyway, I'm confused with your wording: "The package's Create function requires the io.WriteCloser interface." That's not true. The package's Create functions returns a io.WriteCloser, that is, a thingy you can write to and close.

    yourFile, _, err := Create(ctx, "filename", nil)
    // Check err != nil here.
    
    defer func() {
        err := yourFile.Close()
        // Check err != nil here.
    }()
    
    yourFile.Write([]byte("This will be sent to the file immediately."))
    fmt.Fprintln(yourFile, "This too.")
    io.Copy(yourFile, someReader)
    

    This is how interfaces work in Go. They just provide you with a set of methods you can call, hiding the actual implementation from you; and, when you just depend on a particular interface instead of a particular implementation, you can combine in multiple ways, as fmt.Fprintln and io.Copy do.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法