mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-04-25 12:56:23 +02:00
vendor: add sourcegraph/s3cache and dependencies
Adds: - github.com/kr/http/transport - github.com/sqs/s3 - github.com/sqs/s3/s3util - sourcegraph.com/sourcegraph/s3cache
This commit is contained in:
parent
ec96fcbc90
commit
11370ac826
19 changed files with 1228 additions and 0 deletions
26
vendor/sourcegraph.com/sourcegraph/s3cache/LICENSE
generated
vendored
Normal file
26
vendor/sourcegraph.com/sourcegraph/s3cache/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Copyright (c) 2013 The s3cache AUTHORS. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of Sourcegraph Inc. nor the names of its contributors may
|
||||
be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
127
vendor/sourcegraph.com/sourcegraph/s3cache/s3cache.go
generated
vendored
Normal file
127
vendor/sourcegraph.com/sourcegraph/s3cache/s3cache.go
generated
vendored
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// Package s3cache provides an implementation of httpcache.Cache that stores and
|
||||
// retrieves data using Amazon S3.
|
||||
package s3cache // import "sourcegraph.com/sourcegraph/s3cache"
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/sqs/s3"
|
||||
"github.com/sqs/s3/s3util"
|
||||
)
|
||||
|
||||
// Cache objects store and retrieve data using Amazon S3.
|
||||
type Cache struct {
|
||||
// Config is the Amazon S3 configuration.
|
||||
Config s3util.Config
|
||||
|
||||
// BucketURL is the URL to the bucket on Amazon S3, which includes the
|
||||
// bucket name and the AWS region. Example:
|
||||
// "https://s3-us-west-2.amazonaws.com/mybucket".
|
||||
BucketURL string
|
||||
|
||||
// Gzip indicates whether cache entries should be gzipped in Set and
|
||||
// gunzipped in Get. If true, cache entry keys will have the suffix ".gz"
|
||||
// appended.
|
||||
Gzip bool
|
||||
}
|
||||
|
||||
var noLogErrors, _ = strconv.ParseBool(os.Getenv("NO_LOG_S3CACHE_ERRORS"))
|
||||
|
||||
func (c *Cache) Get(key string) (resp []byte, ok bool) {
|
||||
rdr, err := s3util.Open(c.url(key), &c.Config)
|
||||
if err != nil {
|
||||
return []byte{}, false
|
||||
}
|
||||
defer rdr.Close()
|
||||
if c.Gzip {
|
||||
rdr, err = gzip.NewReader(rdr)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
defer rdr.Close()
|
||||
}
|
||||
resp, err = ioutil.ReadAll(rdr)
|
||||
if err != nil {
|
||||
if !noLogErrors {
|
||||
log.Printf("s3cache.Get failed: %s", err)
|
||||
}
|
||||
}
|
||||
return resp, err == nil
|
||||
}
|
||||
|
||||
func (c *Cache) Set(key string, resp []byte) {
|
||||
w, err := s3util.Create(c.url(key), nil, &c.Config)
|
||||
if err != nil {
|
||||
if !noLogErrors {
|
||||
log.Printf("s3util.Create failed: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer w.Close()
|
||||
if c.Gzip {
|
||||
w = gzip.NewWriter(w)
|
||||
defer w.Close()
|
||||
}
|
||||
_, err = w.Write(resp)
|
||||
if err != nil {
|
||||
if !noLogErrors {
|
||||
log.Printf("s3cache.Set failed: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) Delete(key string) {
|
||||
rdr, err := s3util.Delete(c.url(key), &c.Config)
|
||||
if err != nil {
|
||||
if !noLogErrors {
|
||||
log.Printf("s3cache.Delete failed: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer rdr.Close()
|
||||
}
|
||||
|
||||
func (c *Cache) url(key string) string {
|
||||
key = cacheKeyToObjectKey(key)
|
||||
if c.Gzip {
|
||||
key += ".gz"
|
||||
}
|
||||
if strings.HasSuffix(c.BucketURL, "/") {
|
||||
return c.BucketURL + key
|
||||
}
|
||||
return c.BucketURL + "/" + key
|
||||
}
|
||||
|
||||
func cacheKeyToObjectKey(key string) string {
|
||||
h := md5.New()
|
||||
io.WriteString(h, key)
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// New returns a new Cache with underlying storage in Amazon S3. The bucketURL
|
||||
// is the full URL to the bucket on Amazon S3, including the bucket name and AWS
|
||||
// region (e.g., "https://s3-us-west-2.amazonaws.com/mybucket").
|
||||
//
|
||||
// The environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_KEY are used as the AWS
|
||||
// credentials. To use different credentials, modify the returned Cache object
|
||||
// or construct a Cache object manually.
|
||||
func New(bucketURL string) *Cache {
|
||||
return &Cache{
|
||||
Config: s3util.Config{
|
||||
Keys: &s3.Keys{
|
||||
AccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
|
||||
SecretKey: os.Getenv("AWS_SECRET_KEY"),
|
||||
},
|
||||
Service: s3.DefaultService,
|
||||
},
|
||||
BucketURL: bucketURL,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue