Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何使用go脚本在Google云存储中获取我的图像(base64)_Image_Google App Engine_Go_Base64_Google Cloud Storage - Fatal编程技术网

Image 如何使用go脚本在Google云存储中获取我的图像(base64)

Image 如何使用go脚本在Google云存储中获取我的图像(base64),image,google-app-engine,go,base64,google-cloud-storage,Image,Google App Engine,Go,Base64,Google Cloud Storage,我一直在寻找一个示例GAE脚本来获取我的图像,该图像是我从生成的屏幕截图中获得的,并将其保存为json_解码对象,用于谷歌云存储(GCS) 这种方法的原因仅仅是因为我发现这个Kohana模型是向GCS写入文件最方便的方法,尽管我也在寻求其他方式,比如在GOAPI文件被否决时使用GCS将文件写入GCS,以服务它们,而GOAPI文件已被否决,如文档所示 以下是包含截图图像数据(base64)的存储对象的形式,该截图图像数据以公共形式保存在具有对象名称的默认应用程序存储桶中: 我想通过go modul

我一直在寻找一个示例GAE脚本来获取我的图像,该图像是我从生成的屏幕截图中获得的,并将其保存为json_解码对象,用于谷歌云存储(GCS)

这种方法的原因仅仅是因为我发现这个Kohana模型是向GCS写入文件最方便的方法,尽管我也在寻求其他方式,比如在GOAPI文件被否决时使用GCS将文件写入GCS,以服务它们,而GOAPI文件已被否决,如文档所示

以下是包含截图图像数据(base64)的存储对象的形式,该截图图像数据以公共形式保存在具有对象名称的默认应用程序存储桶中:

我想通过
go module
获取此设置为使用自定义url的图像,如下所示,并且我需要它在特定时间内过期,因为我已成功定期更新图像内容本身:

http://myappId.appspot.com/image/thumb/mythumb.jpg

我已在disptach.yaml中设置将所有图像请求发送到我的
go模块
,如下所示:

- url: "*/images/*"
  module: go
handlers:
- url: /images/thumb/.*
  script: _go_app

- url: /images
  static_dir: images
package thumb

import(
    //what to import
    ????
    ????
)

const (
    googleAccessID            = "<serviceAccountEmail>@developer.gserviceaccount.com"
    serviceAccountPEMFilename = "YOUR_SERVICE_ACCOUNT_KEY.pem"
    bucket                    = "myappId.appspot.com"
)

var (
    expiration = time.Now().Add(time.Second * 60) //expire in 60 seconds
)

func init() {
    http.HandleFunc("/images/thumb/", handleThumb)
}

func handleThumb(w http.ResponseWriter, r *http.Request) {
    ctx := cloud.NewContext(appengine.AppID(c), hc)
    ???? //what code to get the string of 'mythumb.jpg' from url
    ???? //what code to get the image stored data from GCS
    ???? //what code to encoce base64 data
    w.Header().Set("Content-Type", "image/jpeg;")    
    fmt.Fprintf(w, "%v", mythumb.jpg)
}
并在go.yaml中设置处理程序以按如下方式继续图像请求:

- url: "*/images/*"
  module: go
handlers:
- url: /images/thumb/.*
  script: _go_app

- url: /images
  static_dir: images
package thumb

import(
    //what to import
    ????
    ????
)

const (
    googleAccessID            = "<serviceAccountEmail>@developer.gserviceaccount.com"
    serviceAccountPEMFilename = "YOUR_SERVICE_ACCOUNT_KEY.pem"
    bucket                    = "myappId.appspot.com"
)

var (
    expiration = time.Now().Add(time.Second * 60) //expire in 60 seconds
)

func init() {
    http.HandleFunc("/images/thumb/", handleThumb)
}

func handleThumb(w http.ResponseWriter, r *http.Request) {
    ctx := cloud.NewContext(appengine.AppID(c), hc)
    ???? //what code to get the string of 'mythumb.jpg' from url
    ???? //what code to get the image stored data from GCS
    ???? //what code to encoce base64 data
    w.Header().Set("Content-Type", "image/jpeg;")    
    fmt.Fprintf(w, "%v", mythumb.jpg)
}
使用这个指令,我得到了所有/images/request(而不是/images/thumb/request)都提供来自静态目录的图像,并且/images/thumb/mythumb.jpg进入模块应用程序

因此,在名为thumb.go的应用程序文件中留下我必须使用的代码(请参见
??
),如下所示:

- url: "*/images/*"
  module: go
handlers:
- url: /images/thumb/.*
  script: _go_app

- url: /images
  static_dir: images
package thumb

import(
    //what to import
    ????
    ????
)

const (
    googleAccessID            = "<serviceAccountEmail>@developer.gserviceaccount.com"
    serviceAccountPEMFilename = "YOUR_SERVICE_ACCOUNT_KEY.pem"
    bucket                    = "myappId.appspot.com"
)

var (
    expiration = time.Now().Add(time.Second * 60) //expire in 60 seconds
)

func init() {
    http.HandleFunc("/images/thumb/", handleThumb)
}

func handleThumb(w http.ResponseWriter, r *http.Request) {
    ctx := cloud.NewContext(appengine.AppID(c), hc)
    ???? //what code to get the string of 'mythumb.jpg' from url
    ???? //what code to get the image stored data from GCS
    ???? //what code to encoce base64 data
    w.Header().Set("Content-Type", "image/jpeg;")    
    fmt.Fprintf(w, "%v", mythumb.jpg)
}
package thumb
进口(
//进口什么
????
????
)
常数(
googleAccessID=“@developer.gserviceaccount.com”
serviceAccountPEMFilename=“您的\u服务\u帐户\u密钥.pem”
bucket=“myappId.appspot.com”
)
变量(
expiration=time.Now().Add(time.Second*60)//60秒后过期
)
func init(){
http.HandleFunc(“/images/thumb/”,handleThumb)
}
func handleThumb(w http.ResponseWriter,r*http.Request){
ctx:=cloud.NewContext(appengine.AppID(c),hc)
??//从url获取“mythumb.jpg”字符串的代码是什么
??//从地面军事系统获取图像存储数据的代码是什么
??//要加密base64数据的代码是什么
w、 Header().Set(“内容类型”、“图像/jpeg;”)
fmt.Fprintf(w,“%v”,mythumb.jpg)
}
我已经从一些例子中获得了很多代码,比如,或者,但是到目前为止还没有一个作品。我也试过一个样品,几乎接近,但也没有发现运气

因此,一般来说,t主要是由于缺少在我标记的行上放置的正确代码,以及要导入的相关库或路径。我还检查了是否缺少所述和所述的内容


我将非常感谢您的帮助和建议。

从我在您的描述中读到的内容来看,似乎唯一相关的部分是实际Go代码中的
行。如果不是这样,请告诉我

第一个??“从url获取'mythumb.jpg'字符串的代码是什么?”? 通过阅读代码,您可以从类似
url的url中提取
mythumb.jpg
http://localhost/images/thumb/mythumb.jpg
。教程中提供了一个工作示例:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
以致

http://localhost:8080/monkeys
印刷品

Hi there, I love monkeys!
第二个??“从地面军事系统获取图像存储数据的代码是什么?”? 您可能希望使用的API方法是

您确实链接到了Google云存储的一个示例,这是一个很好的一般参考,但与您试图解决的问题无关。这个特定的示例是为客户端应用程序(因此出现了
redirectURL=“urn:ietf:wg:oauth:2.0:oob”
行)。此外,此示例使用已弃用/过期的oauth2和存储包

对于希望代表自己访问自己的存储桶的应用程序,最干净(且未被弃用)的方法之一是使用和包

如何使用包使用JSON Web令牌auth进行身份验证的示例如下:

接下来,不要直接使用oauth2
客户端
,而是将该客户端与前面提到的:

service, err := storage.New(client)
if err != nil {
    fatalf(service, "Failed to create service %v", err)
}
注意到与过时的相似之处了吗

在您的处理程序中,您将希望使用。假设您知道
对象
存储桶
的名称,即

直接从上一个示例开始,您可以使用类似于以下内容的代码检索下载链接:

if res, err := service.Objects.Get(bucketName, objectName).Do(); err == nil {
    fmt.Printf("The media download link for %v/%v is %v.\n\n", bucketName, res.Name, res.MediaLink)
} else {
    fatalf(service, "Failed to get %s/%s: %s.", bucketName, objectName, err)
}
然后,取回文件,或者用它做任何你想做的事情。完整示例:

import (
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/jwt"
    "google.golang.org/api/storage/v1"
    "fmt"
)

...

const (
    bucketName = "YOUR_BUCKET_NAME"
    objectName = "mythumb.jpg"
)

func main() {
    conf := &jwt.Config{
        Email: "xxx@developer.com",
        PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
        Subject:    "user@example.com",
        TokenURL:   "https://provider.com/o/oauth2/token",
     }

    client := conf.Client(oauth2.NoContext)

    service, err := storage.New(client)
    if err != nil {
        fatalf(service, "Failed to create service %v", err)
    }

    if res, err := service.Objects.Get(bucketName, objectName).Do(); err == nil {
        fmt.Printf("The media download link for %v/%v is %v.\n\n", bucketName, res.Name, res.MediaLink)
    } else {
        fatalf(service, "Failed to get %s/%s: %s.", bucketName, objectName, err)
    }

    // Go fetch the file, etc.
}
第三个??“使用什么代码加密base64数据”? 这个软件包非常简单。非常简单,包括:


希望能有所帮助。

从我在您的描述中读到的内容来看,似乎唯一相关的部分是实际Go代码中的
行。如果不是这样,请告诉我

第一个??“从url获取'mythumb.jpg'字符串的代码是什么?”? 通过阅读代码,您可以从类似
url的url中提取
mythumb.jpg
http://localhost/images/thumb/mythumb.jpg
。教程中提供了一个工作示例:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
以致

http://localhost:8080/monkeys
印刷品

Hi there, I love monkeys!
第二个??“从地面军事系统获取图像存储数据的代码是什么?”? 您可能希望使用的API方法是

您确实链接到了Google云存储的一个示例,这是一个很好的一般参考,但与您试图解决的问题无关。这个特定的示例是为客户端应用程序(因此出现了
redirectURL=“urn:ietf:wg:oauth:2.0:oob”
行)。此外,此示例使用已弃用/过期的oauth2和存储包

对于希望代表自己访问自己的存储桶的应用程序,最干净(且未被弃用)的方法之一是使用和包

如何使用包使用JSON Web令牌auth进行身份验证的示例如下:

接下来,不使用oaut