我正在尝试使用OCI提供的Go SDK:
我正在尝试使用同一文件夹中的配置文件进行连接,如下所示:
[DEFAULT]
tenancy=ocid1.tenancy.oc1..abcdefg
user=ocid1.user.oc1..abcdefg
fingerprint=00:00:00:00:00.....
key_file="./oci.pem"
region=eu-frankfurt-1
package main
import (
"context"
"encoding/json"
"io"
"net/http"
fdk "github.com/fnproject/fdk-go"
)
func main() {
fdk.Handle(fdk.HandlerFunc(fnHandler))
}
func fnHandler(ctx context.Context, in io.Reader, out io.Writer) {
fnctx, ok := fdk.GetContext(ctx).(fdk.HTTPContext)
if !ok {
fdk.WriteStatus(out, http.StatusBadRequest)
io.WriteString(out, `{"error":"Invalid Request"}`)
return
}
/** Oracle NoSQL and Object Storage Connection */
/**
*
*
*
*
*
*
*
*/
response := struct {
URL string `json:"url"`
Header http.Header `json:"header"`
Config map[string]string `json:"config"`
Message bool `json:"ok"`
}{
URL: fnctx.RequestURL(),
Header: fnctx.Header(),
Config: fnctx.Config(),
Message: ok,
}
fdk.WriteStatus(out, http.StatusCreated)
json.NewEncoder(out).Encode(&response)
}
我找到了这个解决方案。
func createClientNoSQL() (*nosqldb.Client, bool) {
region := "ap-sydney-1"
compartmentID := "ocid1.compartment.oc1......"
tenancy := "ocid1.tenancy.oc1......"
user := "ocid1.user.oc1......"
fingerprint := "...."
privateKeyPassphrase := "" // empty or passphrase
pemString := `-----BEGIN PRIVATE KEY-----
......
-----END PRIVATE KEY-----`
sp, err := iam.NewRawSignatureProvider(tenancy, user, region, fingerprint, compartmentID, pemString, &privateKeyPassphrase)
if err != nil {
return nil, false
}
cfg := nosqldb.Config{
Mode: "cloud",
Region: common.Region(region),
AuthorizationProvider: sp,
}
lcfg := nosqldb.LoggingConfig{
Logger: logger.New(os.Stdout, logger.Warn, false),
}
cfg.LoggingConfig = lcfg
client, err := nosqldb.NewClient(cfg)
if err != nil {
return nil, false
}
return client, true
}