title: How to get my image (base64) in Google-Cloud-Storage with go script
body: <p>I have been looking for an example GAE script in <a href="https://stackoverflow.com/questions/11286534/test-cases-for-go-and-appengine"><strong>go</strong></a> to get my image that I got from the resulted screenshot of <a href="https://developers.google.com/speed/docs/insights/v2/first-app" rel="nofollow noreferrer">PageSpeed Insights</a> and saved it as <em>json_decode</em> object using <a href="https://github.com/kohana/cache/tree/v3.3.0" rel="nofollow noreferrer">Kohana/Cache</a> to <em>Google Cloud Storage (GCS)</em>. </p>
  
  <p>The reason of this method is simply because I found this Kohana model is the most convenient way writing files to GCS, although I am seeking also other way like <a href="https://stackoverflow.com/questions/26987062/how-to-serve-google-cloud-storage-images"><em>this</em></a> to write files to GCS using <a href="https://github.com/voscausa/appengine-gcs-blobstore-python" rel="nofollow noreferrer"><em>Blobstore</em></a> to serve them while the Go API Files has been deprecate as documented <a href="https://cloud.google.com/appengine/docs/go/blobstore/" rel="nofollow noreferrer"><em>here</em></a>.</p>
  
  <p>Here is the form of stored object containing the screenshot image data (base64) which is saved as public in default application bucket with object name <a href="https://www.googleapis.com/storage/v1/b/hyip-investment-monitors.appspot.com/o/images%2Fthumb%2Fmythumb.jpg" rel="nofollow noreferrer"><em>images/thumb/mythumb.jpg</em></a>:</p>
  
  <pre><code>stdClass Object
  (
      [screenshot] =&gt; stdClass Object
          (
              [data] =&gt; _9j_4AAQSkZJRgABAQAAAQABAAD_...= // base64 data
              [height] =&gt; 240
              [mime_type] =&gt; image/jpeg
              [width] =&gt; 320
          )
  
      [otherdata] =&gt; Array
          (
              [..] =&gt; ..
              [..] =&gt; ..
          )
  
  )
  </code></pre>
  
  <p>I want to get this image that set as <a href="https://cloud.google.com/appengine/docs/php/googlestorage/advanced#permissions_caching_and_metadata_options" rel="nofollow noreferrer"><em>public</em></a> using my customized url as below that to be proceed through <code>go module</code> and also I need it to be expired in a certain time because I have managed to update the image content itself regularly:</p>
  
  <blockquote>
    <p><code>http://myappId.appspot.com/image/thumb/mythumb.jpg</code></p>
  </blockquote>
  
  <p>I have set in <em>disptach.yaml</em> to send all image request to my <code>go module</code> as below:</p>
  
  <pre><code>- url: "*/images/*"
    module: go
  </code></pre>
  
  <p>and set the handler in <em>go.yaml</em> to proceed the image request as below:</p>
  
  <pre><code>handlers:
  - url: /images/thumb/.*
    script: _go_app
  
  - url: /images
    static_dir: images
  </code></pre>
  
  <p>Using this directive I have got that all <em>/images/</em> request (other than <em>/images/thumb/</em> request) serve images from the static directory and that <em>/images/thumb/mythumb.jpg</em> goes to the module application. </p>
  
  <p>So left what code I have to use (see <code>????</code>) in my application file named <em>thumb.go</em> as below:</p>
  
  <pre><code>package thumb
  
  import(
      //what to import
      ????
      ????
  )
  
  const (
      googleAccessID            = "&lt;serviceAccountEmail&gt;@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)
  }
  </code></pre>
  
  <p>I have taken many codes from some examples like <a href="https://cloud.google.com/appengine/docs/go/googlecloudstorageclient/getstarted" rel="nofollow noreferrer"><em>this</em></a>, <a href="https://cloud.google.com/storage/docs/access-control#signing-code-go" rel="nofollow noreferrer"><em>this</em></a> or <a href="https://cloud.google.com/storage/docs/json_api/v1/json-api-go-samples" rel="nofollow noreferrer"><em>this</em></a> but could not get one works so far. I have also tried a sample from <a href="http://play.golang.org/p/TkKRROJTfb" rel="nofollow noreferrer"><em>this</em></a> which is almost close to <a href="http://play.golang.org/p/oS2K-Z4NQx" rel="nofollow noreferrer"><em>my case</em></a> but also found no luck.</p>
  
  <p>So in generally t was mainly due to lack on what are the correct code to be put on the line that I marked by <code>????</code> as well the relevant library or path to be imported. I have also checked the <a href="https://cloud.google.com/appengine/docs/go/googlecloudstorageclient/activate" rel="nofollow noreferrer"><em>GCS permission</em></a> if something have been missing as described <a href="https://stackoverflow.com/questions/20294785/how-to-use-google-cloud-storage-for-php"><em>here</em></a> and <a href="https://github.com/GoogleCloudPlatform/gcloud-golang/issues/118" rel="nofollow noreferrer"><em>here</em></a>.</p>
  
  <p>I shall thank you much for your help and advise.</p>