Exemplo n.º 1
0
func s3client() *s3.S3 {
	if client == nil {
		var creds *credentials.Credentials
		if awsAccessKeyID == "" && awsSecretAccessKey == "" {
			client = s3.New(session.New(), EC2RoleAwsConfig())
		} else {
			creds = credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, awsSessionToken)
			if _, err := creds.Get(); err == nil {
				client = s3.New(session.New(), &aws.Config{
					Region:      &awsRegion,
					Credentials: creds,
				})
			}
		}
	}
	return client
}
Exemplo n.º 2
0
// Write will create sitemap xml file into the s3.
func (adp *S3Adapter) Write(loc *Location, data []byte) {
	var reader io.Reader = bytes.NewReader(data)

	if GzipPtn.MatchString(loc.Filename()) {
		var writer *io.PipeWriter

		reader, writer = io.Pipe()
		go func() {
			gz := gzip.NewWriter(writer)
			io.Copy(gz, bytes.NewReader(data))

			gz.Close()
			writer.Close()
		}()
	}

	var creds *credentials.Credentials
	if adp.Credentials == nil {
		creds = credentials.NewEnvCredentials()
	} else {
		creds = adp.Credentials
	}

	creds.Get()

	sess := session.New(&aws.Config{
		Credentials: creds, Region: &adp.Region})

	uploader := s3manager.NewUploader(sess)
	_, err := uploader.Upload(&s3manager.UploadInput{
		Bucket: aws.String(adp.Bucket),
		Key:    aws.String(loc.PathInPublic()),
		ACL:    aws.String(adp.ACL),
		Body:   reader,
	})

	if err != nil {
		log.Fatal("[F] S3 Upload file Error:", err)
	}
}
Exemplo n.º 3
0
// GetAWSCreds returns the appropriate value as the need arises.
//
// evaluated in the following order
// 1. input variable
// 2. Environment variable
// 3. IAM Role
//
// "/.aws/credentials" necessary item increased about that, so it isn't used.
func (c *Config) GetAWSCreds() (*credentials.Credentials, error) {
	var creds *credentials.Credentials
	var err error

	err = nil
	// 1. input variable used
	if c.Aws.Accesskey != "" && c.Aws.SecretKey != "" {
		creds = credentials.NewStaticCredentials(c.Aws.Accesskey, c.Aws.SecretKey, "")
		creds.Expire()
		_, err = creds.Get()
	}

	if err != nil {
		// 2. Environment variable used
		creds = credentials.NewEnvCredentials()
		creds.Expire()
		_, err = creds.Get()

		if err != nil {
			// 3. IAM Role used
			creds = credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{})
			creds.Expire()
			_, err = creds.Get()
		}
	}

	return creds, err
}