func putFiles(bucket *s3.Bucket, c <-chan *Item) { var wg sync.WaitGroup wg.Add(options.Concurrency) for i := 0; i < options.Concurrency; i++ { go func() { for item := range c { func() { f, err := os.Open(item.Path) if err != nil { log.Printf("Pushing %s failed: %s", item.Path, err) } defer f.Close() path := item.Path[len(item.Prefix)+1:] err = bucket.PutReader(options.Put.Prefix+path, f, item.FileInfo.Size(), mime.TypeByExtension(filepath.Ext(item.Path)), s3.BucketOwnerFull) if err != nil { log.Printf("Uploading %s failed: %s", path, err) return } log.Printf("Uploading %s done", path) }() } wg.Done() }() } wg.Wait() }
func upload(bucket *s3.Bucket, checksum, filename string) { f, err := os.Open(filename) handleErr(err) defer f.Close() path := fmt.Sprintf("%s/%s", checksum, filename) fi, err := f.Stat() handleErr(err) err = bucket.PutReader(path, f, fi.Size(), "binary/octet-stream", s3.PublicRead) if err != nil { log.Fatalf("Go makes me sad because of: %s", err) } log.Printf("Uploaded %s", path) }
// Sends this upload to the given S3 bucket func (u *Upload) send(b *s3.Bucket) error { f, err := os.Open(u.LocalFile) if err != nil { return err } defer f.Close() info, err := os.Stat(u.LocalFile) if err != nil { return err } r := bufio.NewReader(f) err = b.PutReader(u.RemoteFile, r, info.Size(), u.GetContentType(), u.Access) if err != nil { return err } return nil }
func syncPath(acl, localPath, path string, bucket *s3.Bucket) { s3Path := strings.Replace(path, localPath, "", -1) file, err := os.Open(path) if err != nil { fmt.Println("Error opening local file, Failed to sync '" + path + "'") return } info, err := file.Stat() if err != nil { fmt.Println("Error stating local file, Failed to sync '" + path + "'") return } length := info.Size() pathSplit := strings.Split(path, ".") ext := pathSplit[len(pathSplit)-1] mtype := mime.TypeByExtension(ext) puterr := bucket.PutReader(s3Path, file, length, mtype, s3.ACL(acl)) if puterr != nil { fmt.Println("Failed to sync: " + s3Path) return } fmt.Println("Synced: " + s3Path) }