- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我一直在 go 中寻找示例 GAE 脚本从 PageSpeed Insights 的结果截图中获取我的图像并使用 Kohana/Cache 将其保存为 json_decode 对象到 Google 云存储 (GCS)。
使用此方法的原因很简单,因为我发现此 Kohana 模型是将文件写入 GCS 的最便捷方式,尽管我也在寻找其他方式,例如 this使用 Blobstore 将文件写入 GCS在 Go API 文件已被弃用时为它们提供服务,如记录 here .
这是包含屏幕截图图像数据 (base64) 的存储对象的形式,该数据在默认应用程序存储桶中以公共(public)方式保存,对象名称为 images/thumb/mythumb.jpg :
stdClass Object
(
[screenshot] => stdClass Object
(
[data] => _9j_4AAQSkZJRgABAQAAAQABAAD_...= // base64 data
[height] => 240
[mime_type] => image/jpeg
[width] => 320
)
[otherdata] => Array
(
[..] => ..
[..] => ..
)
)
我想获取设置为 public 的图像使用我自定义的 url 如下,通过 go module
进行,我还需要它在特定时间过期,因为我已经设法定期更新图像内容本身:
http://myappId.appspot.com/image/thumb/mythumb.jpg
我在 disptach.yaml 中设置将所有图像请求发送到我的 go 模块
,如下所示:
- url: "*/images/*"
module: go
并在 go.yaml 中设置处理程序以按如下方式处理图像请求:
handlers:
- url: /images/thumb/.*
script: _go_app
- url: /images
static_dir: images
使用这个指令,我得到了所有 /images/ 请求(/images/thumb/ 请求除外)提供来自静态目录的图像,并且 /images/thumb/mythumb.jpg 转到模块应用程序。
所以在名为 thumb.go 的应用程序文件中留下了我必须使用的代码(参见 ????
),如下所示:
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)
}
我从一些例子中提取了很多代码,比如 this , this或 this但到目前为止还没有一件作品。我还尝试了 this 中的示例这几乎接近 my case但也没有找到运气。
所以一般情况下,主要是因为缺少在我用????
标记的行上放置的正确代码以及要导入的相关库或路径。我还检查了 GCS permission如果按照描述丢失了某些东西 here和 here .
非常感谢您的帮助和建议。
最佳答案
根据我在您的描述中所读到的内容,似乎唯一相关的部分是实际 Go 代码中的 ????
行。如果情况并非如此,请告诉我。
通过阅读代码,您希望从类似 http://localhost/images/thumb/mythumb.jpg
的 url 中提取 mythumb.jpg
。 Writing Web Applications 提供了一个工作示例教程:
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 方法是 storage.objects.get .
您确实链接到了 JSON API Go Examples 之一对于 Google Cloud Storage,这是一个很好的一般引用,但与您要解决的问题无关。该特定示例是为客户端应用程序组合在一起的(因此 redirectURL = "urn:ietf:wg:oauth:2.0:oob"
行)。此外,此示例使用已弃用/过时的 oauth2 和存储包。
对于想要代表自己访问自己的存储桶的应用程序,最干净(且未弃用)的方法之一是使用 golang/oauth2和 Google APIs Client Library for Go包。
如何通过 golang/oauth2 使用 JSON Web Token auth 进行身份验证的示例包裹是available in the repo :
func ExampleJWTConfig() {
conf := &jwt.Config{
Email: "xxx@developer.com",
// The contents of your RSA private key or your PEM file
// that contains a private key.
// If you have a p12 file instead, you
// can use `openssl` to export the private key into a pem file.
//
// $ openssl pkcs12 -in key.p12 -out key.pem -nodes
//
// It only supports PEM containers with no passphrase.
PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
Subject: "user@example.com",
TokenURL: "https://provider.com/o/oauth2/token",
}
// Initiate an http.Client, the following GET request will be
// authorized and authenticated on the behalf of user@example.com.
client := conf.Client(oauth2.NoContext)
client.Get("...")
}
接下来,不要直接使用 oauth2 客户端
,而是使用带有 Google APIs Client Library for Go 的客户端前面提到:
service, err := storage.New(client)
if err != nil {
fatalf(service, "Failed to create service %v", err)
}
注意与过时的 JSON API Go Examples 的相似之处?
在您的处理程序中,您需要使用 func ObjectsService.Get
获取相关对象。 .假设您知道 object
和 bucket
的名称,即。
直接从前面的示例中,您可以使用类似于下面的代码来检索下载链接:
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.
}
使用 encoding/base64
非常简单包裹。如此简单,他们包括了一个 example :
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := []byte("any + old & data")
str := base64.StdEncoding.EncodeToString(data)
fmt.Println(str)
}
希望对您有所帮助。
关于image - 如何使用 go 脚本在 Google-Cloud-Storage 中获取我的图像(base64),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29148777/
我有一个 Cloud Run 服务,它通过 SQLAlchemy 访问 Cloud SQL 实例.但是,在 Cloud Run 的日志中,我看到 CloudSQL connection failed.
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
在将 docker 容器镜像部署到 Cloud Run 时,我可以选择一个区域,这很好。 Cloud Run 将构建委托(delegate)给 Cloud Build,后者显然会创建两个存储桶来实现这
我正在尝试将 Cloud Functions 用作由 PubSub 触发的异步后台工作程序,并进行更长时间的工作(以分钟为单位)。完整代码在这里https://github.com/zdenulo/c
这是/etc/cloud/cloud.cfg的内容Ubuntu云16.04镜像: # The top level settings are used as module # and system co
如何从 Google Cloud Function 启动 Cloud Dataflow 作业?我想使用 Google Cloud Functions 作为启用跨服务组合的机制。 最佳答案 我已经包含了
我想使用 Cloud Shell 在我的第二代 Cloud Sql 实例上运行数据库迁移。 我找到了一个 example in the docs关于如何使用 gcloud 进行连接.但是当我运行命令时
我正在尝试使用 Google Cloud PubSub和我的 Google Cloud Dataproc群集,我收到如下身份验证范围错误: { "code" : 403, "errors" :
这是我的用例。 我已经有一个以私有(private)模式部署的 Cloud Run 服务。 (与云功能相同的问题) 我正在开发使用此 Cloud Run 的新服务。我在应用程序中使用默认凭据进行身份验
如何连接到 Cloud SQL 上的数据库,而无需在容器中添加我的凭据文件? 最佳答案 使用 UNIX 域套接字 (Java) 从云运行(完全托管)连接到云 SQL At this time Clou
我有一个google-cloud-ml作业,需要从gs存储桶加载numpy .npz文件。我遵循了this example上关于如何从gs加载.npy文件的操作,但是由于.npz文件已压缩,因此它对我
我想创建链接到另一个项目中的 Cloud Source Repository 的 Cloud Build 触发器。但是当我在应该选择存储库的步骤中时,列表是空的。我尝试了不同的许可,但没有运气。谁能告
向 Twilio 发送 SMS 时,Twilio 会向指定的 URL 发送多个请求,以通过 Webhook 提供该 SMS 传送的状态。我想让这个回调异步,所以我开发了一个 Cloud Functio
我需要更改我的项目 ID,因为要验证的 Firebase 身份验证链接在链接上显示了项目 ID,并且由于品牌 reshape ,项目名称已更改。根据我发现的信息,更改项目 ID 似乎不太可能。我正在考
用于部署我的 Angular 应用程序的 CI/CD 管道已关闭,但我看到 Google Cloud Run 在容器镜像更新后没有部署新修订版。 我已将 Cloud Build 设置为在 GitHub
报价https://cloud.google.com/load-balancing/docs/https/setting-up-https-serverless#enabling While Goog
Cloud Spanner 提供了两种不同的 API。 Cloud Spanner 读取与 Cloud Spanner SQL API 之间有什么区别? 最佳答案 在幕后,它们都使用相同的执行机制,因
我是 GCP 堆栈的新手,所以我对用于存储数据的 GCP 技术数量感到非常困惑: https://cloud.google.com/products/storage 虽然上面的文章中没有提到googl
我发现 Google Cloud Functions 的网络出站费用令人惊讶,我正在尝试了解发生这种情况的原因以及如何避免这种情况。 Stackdriver 监控表明有问题的函数是我的 ingest
我使用 Prisma使用 Cloud Run 和 Cloud SQL。在向 prisma.schema 提供 DATABASE_URL 后,它会在运行时抛出一个错误。 Can't reach data
我是一名优秀的程序员,十分优秀!