Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE 50] Adding in an example for multipart upload #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hannahkamundson
Copy link

@hannahkamundson hannahkamundson commented Apr 20, 2024

What: Adding in a walkthrough example for a multipart upload
Link: #50

Why: There currently isn't an example of how to do a multipart upload so I had to do a lot of diggint

Please describe the tests:
No tests just running it locally and verifying it works

Please describe the performance impact: N/A

Code Review Checklist (to be filled out by reviewer)

  • NEW: Are there any Satellite database migrations? Are they forwards and backwards compatible?
  • Does the PR describe what changes are being made?
  • Does the PR describe why the changes are being made?
  • Does the code follow our style guide?
  • Does the code follow our testing guide?
  • Is the PR appropriately sized? (If it could be broken into smaller PRs it should be)
  • Does the new code have enough tests? (every PR should have tests or justification otherwise. Bug-fix PRs especially)
  • Does the new code have enough documentation that answers "how do I use it?" and "what does it do?"? (both source documentation and higher level, diagrams?)
  • Does any documentation need updating?
  • Do the database access patterns make sense?

@hannahkamundson
Copy link
Author

@Erikvv, let me know if you have any suggestions!

Copy link
Contributor

@Erikvv Erikvv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. I don't have merge permission but I'll review.

Suggestion: have two examples: one for upload+share, and one for multipart upload.

@@ -129,6 +140,114 @@ func CreatePublicSharedLink(ctx context.Context, accessGrant, bucketName, object
return url, nil
}

func MultipartUpload(ctx context.Context, accessGrant, bucketName, objectKey string, contents1, contents2 []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verb first, so UploadMultipart

return nil
}

func createMultipartData() (contents1, contents2 []byte) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Pascal case, CreateMultipartData

@hannahkamundson
Copy link
Author

Okay, thank you @Erikvv! I will make the changes. Do you know who has merge privileges?

Copy link
Member

@egonelbre egonelbre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, just a bunch of nitpicks.

We can do those ourselves as well if you are busy... but if you'd like to do these yourself, then I'm definitely not stopping you :).

Thank you.

func UploadAndDownloadData(ctx context.Context,
accessGrant, bucketName, uploadKey string, dataToUpload []byte) error {

func CreateProjectAndConfirmBucket(ctx context.Context, accessGrant, bucketName string) (*uplink.Project, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func CreateProjectAndConfirmBucket(ctx context.Context, accessGrant, bucketName string) (*uplink.Project, error) {
// OpenProjectAndEnsureBucket shows how to open a project and check that the bucket exists and create if it doesn't.
func OpenProjectAndEnsureBucket(ctx context.Context, accessGrant, bucketName string) (*uplink.Project, error) {

Note, it's "OpenProject", because it actually doesn't create a new project when it's missing -- just opens a new connection to the project.

Similar reason for the "EnsureBucket" -- "Confirm" would somewhat imply that it would fail if the bucket doesn't exist.

@@ -129,6 +140,114 @@ func CreatePublicSharedLink(ctx context.Context, accessGrant, bucketName, object
return url, nil
}

func MultipartUpload(ctx context.Context, accessGrant, bucketName, objectKey string, contents1, contents2 []byte) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func MultipartUpload(ctx context.Context, accessGrant, bucketName, objectKey string, contents1, contents2 []byte) error {
// MultipartUpload demonstrates how to upload object in multiple parts.
func MultipartUpload(ctx context.Context, accessGrant, bucketName, objectKey string, first, second []byte) error {

The change from contents1 -> to first, because when reading code the suffix numbers can get confusing. Especially when they are interleaved in places. It's not a big problem in this case, but we like to keep this consistent. (Also the relevant error messages need updating as well.)

Using firstPart or firstContent would be also fine.

// Write the contents to part 1 upload
_, err = part1.Write(contents1)
if err != nil {
return fmt.Errorf("unable to write to part 1: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("unable to write to part 1: %v", err)
_ = part1.Close()
return fmt.Errorf("unable to write to part 1: %v", err)

// Write the contents to part 2 upload
_, err = part2.Write(contents2)
if err != nil {
return fmt.Errorf("unable to write to part 2: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("unable to write to part 2: %v", err)
_ = part2.Close()
return fmt.Errorf("unable to write to part 2: %v", err)

return nil
}

func createMultipartData() (contents1, contents2 []byte) {
Copy link
Member

@egonelbre egonelbre Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this function you can use:

firstPart := bytes.Repeat([]byte("abcdefghijklm"), 1e6)
secondPart := bytes.Repeat([]byte("0123456789"), 1e6)

Comment on lines +273 to +282

largeObjectKey := "foo/bar/large"

largeContents1, largeContents2 := createMultipartData()

err = MultipartUpload(ctx, *accessGrant, bucketName, largeObjectKey, largeContents1, largeContents2)
if err != nil {
fmt.Fprintln(os.Stderr, "creating mulipart upload failed:", err)
os.Exit(1)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
largeObjectKey := "foo/bar/large"
largeContents1, largeContents2 := createMultipartData()
err = MultipartUpload(ctx, *accessGrant, bucketName, largeObjectKey, largeContents1, largeContents2)
if err != nil {
fmt.Fprintln(os.Stderr, "creating mulipart upload failed:", err)
os.Exit(1)
}
largeObjectKey := "foo/bar/large"
largeFirstPart := bytes.Repeat([]byte("abcdefghijklm"), 1e6)
largeSecondPart := bytes.Repeat([]byte("0123456789"), 1e6)
err = MultipartUpload(ctx, *accessGrant, bucketName, largeObjectKey, largeFirstPart, largeSecondPart)
if err != nil {
fmt.Fprintln(os.Stderr, "creating mulipart upload failed:", err)
os.Exit(1)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants