當前位置: 首頁>>代碼示例>>Golang>>正文


Golang PutObjectInput.Body方法代碼示例

本文整理匯總了Golang中github.com/aws/aws-sdk-go/service/s3.PutObjectInput.Body方法的典型用法代碼示例。如果您正苦於以下問題:Golang PutObjectInput.Body方法的具體用法?Golang PutObjectInput.Body怎麽用?Golang PutObjectInput.Body使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/aws/aws-sdk-go/service/s3.PutObjectInput的用法示例。


在下文中一共展示了PutObjectInput.Body方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestS3Connection

func (s *S3Suite) TestS3Connection(c *C) {
	createBucket := s3.CreateBucketInput{Bucket: aws.String("bucket")}
	_, err := s.svc.CreateBucket(&createBucket)
	c.Assert(err, IsNil)

	conn := NewS3Connection(credentials.AnonymousCredentials, "bucket", "prefix", s.region, s.endpoint)
	files, err := conn.ListDir("path", nil)
	c.Assert(err, IsNil)
	c.Assert(len(files.Files), Equals, 0)

	buffer := bytes.NewReader(make([]byte, 100))
	putObject := s3.PutObjectInput{}
	putObject.Bucket = aws.String("bucket")
	putObject.Key = aws.String("prefix/banana")
	putObject.Body = buffer
	s.svc.PutObject(&putObject)

	putObject = s3.PutObjectInput{}
	putObject.Bucket = aws.String("bucket")
	putObject.Key = aws.String("prefix/sampledir/")
	putObject.Body = bytes.NewReader(make([]byte, 0))
	s.svc.PutObject(&putObject)

	putObject = s3.PutObjectInput{}
	putObject.Bucket = aws.String("bucket")
	putObject.Key = aws.String("prefix/sampledir/a")
	putObject.Body = bytes.NewReader(make([]byte, 0))
	s.svc.PutObject(&putObject)

	putObject = s3.PutObjectInput{}
	putObject.Bucket = aws.String("bucket")
	putObject.Key = aws.String("prefix/sampledir/b")
	putObject.Body = bytes.NewReader(make([]byte, 0))
	s.svc.PutObject(&putObject)

	files, err = conn.ListDir("", nil)
	fmt.Printf("files=%s\n", files)
	c.Assert(err, IsNil)
	c.Assert(len(files.Files), Equals, 2)
	f := files.Files[1]
	c.Assert(f.Name, Equals, "banana")
	c.Assert(f.Size, Equals, uint64(100))
	c.Assert(f.IsDir, Equals, false)

	f = files.Files[0]
	c.Assert(f.Name, Equals, "sampledir")
	c.Assert(f.Size, Equals, uint64(0))
	c.Assert(f.IsDir, Equals, true)
}
開發者ID:pgm,項目名稱:singleply,代碼行數:49,代碼來源:s3_test.go

示例2: TestS3ModifyDuringRead

func (s *S3Suite) TestS3ModifyDuringRead(c *C) {
	createBucket := s3.CreateBucketInput{Bucket: aws.String("modified")}
	_, err := s.svc.CreateBucket(&createBucket)
	c.Assert(err, IsNil)

	// upload a file
	buffer := bytes.NewReader(make([]byte, 100))
	putObject := s3.PutObjectInput{}
	putObject.Bucket = aws.String("modified")
	putObject.Key = aws.String("prefix/banana")
	putObject.Body = buffer
	s.svc.PutObject(&putObject)

	// make sure ListDir sees it
	conn := NewS3Connection(credentials.AnonymousCredentials, "modified", "prefix", s.region, s.endpoint)
	files, err := conn.ListDir("", nil)
	c.Assert(err, IsNil)
	c.Assert(len(files.Files), Equals, 1)
	f := files.Files[0]
	c.Assert(f.Name, Equals, "banana")

	localFile, _ := ioutil.TempFile(c.MkDir(), "local")
	localPath := localFile.Name()
	localFile.Close()

	status := &NullStatusCallback{}

	// Perform a read
	region, err := conn.PrepareForRead("banana", f.Etag, localPath, 0, 10, status)
	c.Assert(err, IsNil)
	c.Assert(region.Offset, Equals, uint64(0))
	c.Assert(region.Length, Equals, uint64(10))

	// upload new version of object
	buffer = bytes.NewReader(make([]byte, 101))
	putObject = s3.PutObjectInput{}
	putObject.Bucket = aws.String("modified")
	putObject.Key = aws.String("prefix/banana")
	putObject.Body = buffer
	s.svc.PutObject(&putObject)

	// try a read, and we should get a failure because data changed, and hence Etag
	_, err = conn.PrepareForRead("banana", f.Etag, localPath, 10, 20, status)
	c.Assert(err, Equals, UpdateDetected)
}
開發者ID:pgm,項目名稱:singleply,代碼行數:45,代碼來源:s3_test.go

示例3: PutObjectRequest

// PutObjectRequest creates a temp file to encrypt the contents into. It then streams
// that data to S3.
//
// Example:
//	svc := s3crypto.New(session.New(), s3crypto.AESGCMContentCipherBuilder(handler))
//	req, out := svc.PutObjectRequest(&s3.PutObjectInput {
//	  Key: aws.String("testKey"),
//	  Bucket: aws.String("testBucket"),
//	  Body: bytes.NewBuffer("test data"),
//	})
//	err := req.Send()
func (c *EncryptionClient) PutObjectRequest(input *s3.PutObjectInput) (*request.Request, *s3.PutObjectOutput) {
	req, out := c.S3Client.PutObjectRequest(input)

	// Get Size of file
	n, err := input.Body.Seek(0, 2)
	if err != nil {
		req.Error = err
		return req, out
	}
	input.Body.Seek(0, 0)

	dst, err := getWriterStore(req, c.TempFolderPath, n >= c.MinFileSize)
	if err != nil {
		req.Error = err
		return req, out
	}

	encryptor, err := c.ContentCipherBuilder.ContentCipher()
	req.Handlers.Build.PushFront(func(r *request.Request) {
		if err != nil {
			r.Error = err
			return
		}

		md5 := newMD5Reader(input.Body)
		sha := newSHA256Writer(dst)
		reader, err := encryptor.EncryptContents(md5)
		if err != nil {
			r.Error = err
			return
		}

		_, err = io.Copy(sha, reader)
		if err != nil {
			r.Error = err
			return
		}

		data := encryptor.GetCipherData()
		env, err := encodeMeta(md5, data)
		if err != nil {
			r.Error = err
			return
		}

		shaHex := hex.EncodeToString(sha.GetValue())
		req.HTTPRequest.Header.Set("X-Amz-Content-Sha256", shaHex)

		dst.Seek(0, 0)
		input.Body = dst

		err = c.SaveStrategy.Save(env, r)
		r.Error = err
	})

	return req, out
}
開發者ID:acquia,項目名稱:fifo2kinesis,代碼行數:68,代碼來源:encryption_client.go


注:本文中的github.com/aws/aws-sdk-go/service/s3.PutObjectInput.Body方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。