Example #1
0
func TestMultipartUpload(t *testing.T) {
	initRet, initErr := svc.CreateMultipartUpload(&s3.CreateMultipartUploadInput{
		Bucket:      aws.String(bucket),
		Key:         aws.String(key),
		ACL:         aws.String("public-read"),
		ContentType: aws.String("image/jpeg"),
	})
	assert.NoError(t, initErr)
	assert.Equal(t, bucket, *initRet.Bucket)
	assert.Equal(t, key, *initRet.Key)
	assert.NotNil(t, *initRet.UploadID)

	uploadId := *initRet.UploadID

	upRet, upErr := svc.UploadPart(&s3.UploadPartInput{
		Bucket:        aws.String(bucket),
		Key:           aws.String(key),
		PartNumber:    aws.Long(1),
		UploadID:      aws.String(uploadId),
		Body:          strings.NewReader(content),
		ContentLength: aws.Long(int64(len(content))),
	})
	assert.NoError(t, upErr)
	assert.NotNil(t, upRet.ETag)

	listRet, listErr := svc.ListParts(&s3.ListPartsInput{
		Bucket:   aws.String(bucket),
		Key:      aws.String(key),
		UploadID: aws.String(uploadId),
	})
	assert.NoError(t, listErr)
	assert.Equal(t, bucket, *listRet.Bucket)
	assert.Equal(t, key, *listRet.Key)
	assert.False(t, *listRet.IsTruncated)
	assert.Equal(t, uploadId, *listRet.UploadID)
	parts := listRet.Parts
	assert.Equal(t, 1, len(parts))
	part := parts[0]

	compParts := []*s3.CompletedPart{&s3.CompletedPart{PartNumber: part.PartNumber, ETag: part.ETag}}
	compRet, compErr := svc.CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
		Bucket:   aws.String(bucket),
		Key:      aws.String(key),
		UploadID: aws.String(uploadId),
		MultipartUpload: &s3.CompletedMultipartUpload{
			Parts: compParts,
		},
	})
	assert.NoError(t, compErr)
	assert.Equal(t, bucket, *compRet.Bucket)
	assert.Equal(t, key, *compRet.Key)
}
Example #2
0
func TestPutObjectPresignedUrl(t *testing.T) {
	params := &s3.PutObjectInput{
		Bucket:           aws.String(bucket),                    // bucket名称
		Key:              aws.String(key),                       // object key
		ACL:              aws.String("public-read"),             //设置ACL
		ContentType:      aws.String("application/ocet-stream"), //设置文件的content-type
		ContentMaxLength: aws.Long(20),                          //设置允许的最大长度,对应的header:x-amz-content-maxlength
	}
	resp, err := svc.PutObjectPresignedUrl(params, 1444370289000000000) //第二个参数为外链过期时间,第二个参数为time.Duration类型
	if err != nil {
		panic(err)
	}

	httpReq, _ := http.NewRequest("PUT", "", strings.NewReader("123"))
	httpReq.URL = resp
	httpReq.Header["x-amz-acl"] = []string{"public-read"}
	httpReq.Header["x-amz-content-maxlength"] = []string{"20"}
	httpReq.Header.Add("Content-Type", "application/ocet-stream")
	fmt.Println(httpReq)
	httpRep, err := http.DefaultClient.Do(httpReq)
	fmt.Println(httpRep)
	if err != nil {
		panic(err)
	}
	assert.Equal(t, "200 OK", httpRep.Status)
}
Example #3
0
func TestListObjects(t *testing.T) {
	putObjectSimple()
	objects, err := svc.ListObjects(&s3.ListObjectsInput{
		Bucket:    aws.String(bucket),
		Delimiter: aws.String("/"),
		MaxKeys:   aws.Long(999),
		Prefix:    aws.String(""),
	})
	assert.NoError(t, err)
	assert.Equal(t, "/", *objects.Delimiter)
	assert.Equal(t, *aws.Long(999), *objects.MaxKeys)
	assert.Equal(t, "", *objects.Prefix)
	assert.False(t, *objects.IsTruncated)

	objects1, err := svc.ListObjects(&s3.ListObjectsInput{
		Bucket: aws.String(bucket),
	})
	assert.NoError(t, err)
	objectList := objects1.Contents
	found := false
	for i := 0; i < len(objectList); i++ {
		object := objectList[i]
		assert.Equal(t, "STANDARD", *object.StorageClass)
		if *object.Key == key {
			found = true
		}
	}
	assert.True(t, found, "expected found "+key+"in object listing")
}
func BenchmarkCodegenIterator(b *testing.B) {
	reqNum := 0
	db := benchDb()
	db.Handlers.Unmarshal.PushBack(func(r *aws.Request) {
		r.Data = benchResps[reqNum]
		reqNum++
	})

	input := &dynamodb.ListTablesInput{Limit: aws.Long(2)}
	iter := func(fn func(*dynamodb.ListTablesOutput, bool) bool) error {
		page, _ := db.ListTablesRequest(input)
		for ; page != nil; page = page.NextPage() {
			page.Send()
			out := page.Data.(*dynamodb.ListTablesOutput)
			if result := fn(out, !page.HasNextPage()); page.Error != nil || !result {
				return page.Error
			}
		}
		return nil
	}

	for i := 0; i < b.N; i++ {
		reqNum = 0
		iter(func(p *dynamodb.ListTablesOutput, last bool) bool {
			return true
		})
	}
}
// Use DynamoDB methods for simplicity
func TestPaginationEachPage(t *testing.T) {
	db := dynamodb.New(nil)
	tokens, pages, numPages, gotToEnd := []string{}, []string{}, 0, false

	reqNum := 0
	resps := []*dynamodb.ListTablesOutput{
		{TableNames: []*string{aws.String("Table1"), aws.String("Table2")}, LastEvaluatedTableName: aws.String("Table2")},
		{TableNames: []*string{aws.String("Table3"), aws.String("Table4")}, LastEvaluatedTableName: aws.String("Table4")},
		{TableNames: []*string{aws.String("Table5")}},
	}

	db.Handlers.Send.Clear() // mock sending
	db.Handlers.Unmarshal.Clear()
	db.Handlers.UnmarshalMeta.Clear()
	db.Handlers.ValidateResponse.Clear()
	db.Handlers.Build.PushBack(func(r *aws.Request) {
		in := r.Params.(*dynamodb.ListTablesInput)
		if in == nil {
			tokens = append(tokens, "")
		} else if in.ExclusiveStartTableName != nil {
			tokens = append(tokens, *in.ExclusiveStartTableName)
		}
	})
	db.Handlers.Unmarshal.PushBack(func(r *aws.Request) {
		r.Data = resps[reqNum]
		reqNum++
	})

	params := &dynamodb.ListTablesInput{Limit: aws.Long(2)}
	req, _ := db.ListTablesRequest(params)
	err := req.EachPage(func(p interface{}, last bool) bool {
		numPages++
		for _, t := range p.(*dynamodb.ListTablesOutput).TableNames {
			pages = append(pages, *t)
		}
		if last {
			if gotToEnd {
				assert.Fail(t, "last=true happened twice")
			}
			gotToEnd = true
		}

		return true
	})

	assert.Equal(t, []string{"Table2", "Table4"}, tokens)
	assert.Equal(t, []string{"Table1", "Table2", "Table3", "Table4", "Table5"}, pages)
	assert.Equal(t, 3, numPages)
	assert.True(t, gotToEnd)
	assert.Nil(t, err)
}
func BenchmarkEachPageIterator(b *testing.B) {
	reqNum := 0
	db := benchDb()
	db.Handlers.Unmarshal.PushBack(func(r *aws.Request) {
		r.Data = benchResps[reqNum]
		reqNum++
	})

	input := &dynamodb.ListTablesInput{Limit: aws.Long(2)}
	for i := 0; i < b.N; i++ {
		reqNum = 0
		req, _ := db.ListTablesRequest(input)
		req.EachPage(func(p interface{}, last bool) bool {
			return true
		})
	}
}
// Use DynamoDB methods for simplicity
func TestPaginationEarlyExit(t *testing.T) {
	db := dynamodb.New(nil)
	numPages, gotToEnd := 0, false

	reqNum := 0
	resps := []*dynamodb.ListTablesOutput{
		{TableNames: []*string{aws.String("Table1"), aws.String("Table2")}, LastEvaluatedTableName: aws.String("Table2")},
		{TableNames: []*string{aws.String("Table3"), aws.String("Table4")}, LastEvaluatedTableName: aws.String("Table4")},
		{TableNames: []*string{aws.String("Table5")}},
	}

	db.Handlers.Send.Clear() // mock sending
	db.Handlers.Unmarshal.Clear()
	db.Handlers.UnmarshalMeta.Clear()
	db.Handlers.ValidateResponse.Clear()
	db.Handlers.Unmarshal.PushBack(func(r *aws.Request) {
		r.Data = resps[reqNum]
		reqNum++
	})

	params := &dynamodb.ListTablesInput{Limit: aws.Long(2)}
	err := db.ListTablesPages(params, func(p *dynamodb.ListTablesOutput, last bool) bool {
		numPages++
		if numPages == 2 {
			return false
		}
		if last {
			if gotToEnd {
				assert.Fail(t, "last=true happened twice")
			}
			gotToEnd = true
		}
		return true
	})

	assert.Equal(t, 2, numPages)
	assert.False(t, gotToEnd)
	assert.Nil(t, err)
}