Skip to content

Instantly share code, notes, and snippets.

@prl900
Created April 6, 2018 05:05
Show Gist options
  • Save prl900/b95cf8d835f9837db2a95c65841cacef to your computer and use it in GitHub Desktop.
Save prl900/b95cf8d835f9837db2a95c65841cacef to your computer and use it in GitHub Desktop.
Upload local copy of Modis tiles
package main
import (
"fmt"
"io/ioutil"
//"os"
"path"
"cloud.google.com/go/storage"
"golang.org/x/net/context"
)
const localDir = "/Users/pablo/Desktop/modis/"
const bktName = "modisimple"
func WriteObject(bktName, objName string, contents []byte) error {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
//projectID := "YOUR_PROJECT_ID"
//projectID := "nci-gce"
// Creates a client.
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("Failed to create client: %v", err)
}
// Creates a Bucket instance.
bucket := client.Bucket(bktName)
w := bucket.Object(objName).NewWriter(ctx)
w.ContentType = "application/octet-stream"
if _, err := w.Write([]byte(contents)); err != nil {
return fmt.Errorf("Failed to write object to bucket: %v", err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("Failed to close writer to bucket: %v", err)
}
// Close the client when finished.
if err := client.Close(); err != nil {
return fmt.Errorf("Failed to close client: %v", err)
}
return nil
}
func main() {
files, err := ioutil.ReadDir(localDir)
if err != nil {
panic(err)
}
for _, f := range files {
fName := f.Name()
if fName[:4] == "MCD4" {
shortName := fName[:27] + fName[41:]
fmt.Println(shortName)
data, err := ioutil.ReadFile(path.Join(localDir, f.Name()))
if err != nil {
panic(err)
}
err = WriteObject(bktName, shortName, data)
if err != nil {
panic(err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment