Example #1
0
//CopyFileToS3 - Responsible for copying a file to S3
func (t *Task) CopyFileToS3() (err error) {
	start := time.Now()

	f := history.S3FileHistory{}
	f.Status = "error" //Changed at the end of the function if successful
	f.Bucket = rdpgs3.BucketName
	defer func() {
		f.Duration = int(time.Since(start).Seconds())
		insertErr := history.InsertS3History(f)
		if insertErr != nil {
			log.Error(fmt.Sprintf("tasks.CopyFileToS3 ! insertS3History erred : %s", err.Error()))
		}
	}()

	taskParams := []byte(t.Data)
	fm := S3FileMetadata{}
	err = json.Unmarshal(taskParams, &fm)
	if err != nil {
		log.Error(fmt.Sprintf("tasks.CopyFileToS3() json.Unmarshal() ! %s", err))
		return err
	}

	//Log results to backups.file_history
	f.Source = fm.Location
	f.Target = fm.Location
	f.DBName = fm.DBName
	f.Node = fm.Node
	creds := credentials.NewStaticCredentials(rdpgs3.AWSAccessKey, rdpgs3.AWSSecretKey, rdpgs3.Token)

	config := &aws.Config{
		Region:           &rdpgs3.AWSRegion,
		Endpoint:         &rdpgs3.Endpoint,
		S3ForcePathStyle: &rdpgs3.S3ForcePathStyle,
		Credentials:      creds,
	}

	s3client := s3.New(config)

	file, err := os.Open(fm.Location)
	if err != nil {
		log.Error(fmt.Sprintf("tasks.CopyFileToS3() Error attempting to open file %s ! %s", fm.Location, err))
		return err
	}

	defer file.Close()

	fileInfo, _ := file.Stat()
	f.Size = fileInfo.Size()
	f.FileName = fileInfo.Name()
	buffer := make([]byte, f.Size)
	file.Read(buffer)
	fileBytes := bytes.NewReader(buffer) // convert to io.ReadSeeker type
	fileType := http.DetectContentType(buffer)

	s3params := &s3.PutObjectInput{
		Bucket:        aws.String(rdpgs3.BucketName), // required
		Key:           aws.String(fm.Location),       // required
		ACL:           aws.String("public-read"),     //other values: http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#CannedACL
		Body:          fileBytes,
		ContentLength: aws.Int64(f.Size),
		ContentType:   aws.String(fileType),
		Metadata: map[string]*string{
			"Key": aws.String("MetadataValue"), //required
		},
		// see more at http://godoc.org/github.com/aws/aws-sdk-go/service/s3#S3.PutObject
	}

	result, err := s3client.PutObject(s3params)
	log.Trace(fmt.Sprintf("tasks.CopyFileToS3() Copy file to S3 result > %s ", result))

	if err != nil {
		log.Error(fmt.Sprintf("tasks.CopyFileToS3() AWS General Error ! %s", err))
		if awsErr, ok := err.(awserr.Error); ok {
			// Generic AWS Error with Code, Message, and original error (if any)
			log.Error(fmt.Sprintf("tasks.CopyFileToS3() AWS Error %s !! %s ! %s", awsErr.Code(), awsErr.Message(), awsErr.OrigErr()))
			if reqErr, ok := err.(awserr.RequestFailure); ok {
				// A service error occurred
				log.Error(fmt.Sprintf("tasks.CopyFileToS3() AWS Service Error %s !!! %s !! %s ! %s", reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID()))
			}
		} else {
			// This case should never be hit, the SDK should always return an
			// error which satisfies the awserr.Error interface.
			log.Error(fmt.Sprintf("tasks.CopyFileToS3() General AWS Error %s ! ", err.Error()))
		}
	}

	return
}