import ( "fmt" "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud" "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/openstack" "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/pagination" "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/rackspace" "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/rackspace/objectstorage/v1/objects" ) func main() { // initialize the ServiceClient with Rackspace identity credentials opts, err := openstack.AuthOptionsFromEnv() if err != nil { panic(err) } provider, err := rackspace.AuthenticatedClient(opts) if err != nil { panic(err) } client, err := openstack.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: "ord", }) if err != nil { panic(err) } // upload a file to a container containerName := "example-container" fileName := "example-file.jpg" imagePath := "./path/to/image.jpg" fileContent, err := os.Open(imagePath) if err != nil { panic(err) } defer fileContent.Close() result := objects.Create(client, containerName, fileName, fileContent, nil) if result.Err != nil { panic(result.Err) } fmt.Printf("Uploaded image %s to container %s\n", fileName, containerName) }
import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud" ) func main() { // initialize the ServiceClient with custom API endpoint and authentication credentials apiEndpoint := "https://example.com/api/v1" accessKey := "my-access-key" secretKey := "my-secret-key" client := &gophercloud.ServiceClient{ Endpoint: apiEndpoint, TokenID: accessKey+":"+secretKey, } // build a JSON payload requestBody := map[string]interface{}{ "foo": "bar", "baz": []int{1, 2, 3}, "qux": map[string]string{ "hello": "world", }, } jsonPayload, err := json.Marshal(requestBody) if err != nil { panic(err) } // send the JSON payload as a POST request to the custom API endpoint req, err := http.NewRequest("POST", apiEndpoint, bytes.NewBuffer(jsonPayload)) if err != nil { panic(err) } req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() // parse the response body as JSON responseBody, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } var responsePayload map[string]interface{} err = json.Unmarshal(responseBody, &responsePayload) if err != nil { panic(err) } fmt.Printf("API response: %v\n", responsePayload) }