Golang : Get S3 or CloudFront object or file information
Here is a simple example on how to get the S3, CloudFront object (in fact, any file accessible from URL) information. Information such as Etag, X-Amz-Storage-Class, Last-Modified, Cache-Control or Content-Type values. This information is also known as metadata.
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
url := "https://d1ohg4ss876yi2.cloudfront.net/preview/golang.png"
response, err := http.Head(url)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Is our request ok?
if response.StatusCode != http.StatusOK {
fmt.Println(response.Status)
os.Exit(1)
// exit if not ok
}
for k, v := range response.Header {
fmt.Println("Header :", k, " : ", v)
}
}
Sample output :
Header : Age : [1198]
Header : Etag : ["11ef76eda4cd556455088d69eb95bdc2"]
Header : Server : [AmazonS3]
Header : Date : [Wed, 18 Nov 2015 08:19:31 GMT]
Header : Cache-Control : [max-age=315360000]
Header : Last-Modified : [Fri, 13 Nov 2015 09:44:33 GMT]
Header : X-Cache : [Hit from cloudfront]
Header : Via : [1.1 04fb12f896eac18e91eac3e60e276ed8.cloudfront.net (CloudFront)]
Header : X-Amz-Cf-Id : [J7qrcfGsa9vssCAkavL8v8zqn1JMU4PneP2aMY4LyEKSLIRRREfg==]
Header : Content-Length : [113857]
Header : Connection : [keep-alive]
Header : Accept-Ranges : [bytes]
Header : Content-Type : [image/png]
Header : X-Amz-Storage-Class : [REDUCED_REDUNDANCY]
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.5k Golang : Get expvar(export variables) to work with multiplexer
+11.1k Golang : Concatenate (combine) buffer data example
+16.1k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+21.8k Golang : How to run Golang application such as web server in the background or as daemon?
+11.2k Golang : Handle API query by curl with Gorilla Queries example
+14.9k Golang : How to add color to string?
+6.2k PHP : Proper way to get UTF-8 character or string length
+8.9k Golang : Create and shuffle deck of cards example
+36.3k Golang : Display float in 2 decimal points and rounding up or down
+5.1k Golang : How to deal with configuration data?
+5.1k Unix/Linux/MacOSx : How to remove an environment variable ?
+18.2k Golang : Find IP address from string