Exemple #1
0
func testBucket(s *s3.S3) *s3.Bucket {
	// Watch out! If this function is corrupted and made to match with something
	// people own, killBucket will happily remove *everything* inside the bucket.
	key := s.Auth.AccessKey
	if len(key) >= 8 {
		key = s.Auth.AccessKey[:8]
	}
	return s.Bucket(fmt.Sprintf("goamz-%s-%s", s.Region.Name, key))
}
Exemple #2
0
func syncFileFromS3(s3_conn *s3.S3, file FileToSync) bool {

	bucketName, s3File := parseS3Path(file.RemotePath)

	bucket := s3_conn.Bucket(bucketName)

	// ts3, err := getS3FileLastModified(bucket, s3File)
	s3Hash, err := getS3FileHash(bucket, s3File)
	if err != nil {
		if err == ErrFileDoesNotExistS3 {
			// If the file doesn't exist in S3, then we have nothing else to do...
			log.Printf("%v: %v", err.Error(), s3File)
			return false
		} else {
			log.Printf("%v: %v", err.Error(), file)
			return false
		}
	}

	localHash, err := getLocalFileHash(file.LocalPath)
	if err != nil {

		if os.IsNotExist(err) {
			// If the file doesn't exist locally then give it a fake hash
			localHash = "fakeHash"
		} else {
			log.Printf("%v: %v", err.Error(), file)
			return false
		}
	}

	if s3Hash == localHash {
		// log.Printf("%v in S3 and Local are equal.", file.RemotePath)
		return false
	} else {
		// log.Printf("%s in S3 is newer. Updating...", file.RemotePath)
	}

	data, err := bucket.Get(s3File)
	if err != nil {
		log.Printf("%v %v", err.Error(), file)
		return false
	}

	// log.Printf("Updating local file: %v", file.LocalPath)
	err = ioutil.WriteFile(file.LocalPath, data, 0664)
	if err != nil {
		log.Fatalf("Error Writing file %v", file.LocalPath)
	}

	return true

}