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

Expose more functions #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,30 @@ console.log(upload);
*/
```

### Generate signed URL with expiration time
### Generate GET signed URL with expiration time
[More about signed URLS on R2](https://developers.cloudflare.com/r2/examples/aws/aws-sdk-js/#generate-presigned-urls)

```javascript
// Generate signed link that expires after 3600 seconds.
// Generate GET signed link that expires after 3600 seconds.
const signedUrl = await bucket.getObjectSignedUrl('destination_file_name.ext', 3600);
console.log(signedUrl);
/*
https://bucket-name.cloudflare-account-id.r2.cloudflarestorage.com/destination_file_name.ext?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=...&X-Amz-Date=...&X-Amz-Expires=60&X-Amz-Signature=...&X-Amz-SignedHeaders=host&x-id=GetObject
*/
```

### Generate PUT signed URL with expiration time
[More about signed URLS on R2](https://developers.cloudflare.com/r2/examples/aws/aws-sdk-js/#generate-presigned-urls)

```javascript
// Generate signed link that expires after 3600 seconds.
const signedUrl = await bucket.putObjectSignedUrl('destination_file_name.ext', 3600);
console.log(signedUrl);
/*
https://bucket-name.cloudflare-account-id.r2.cloudflarestorage.com/destination_file_name.ext?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...>&X-Amz-Expires=3600&X-Amz-Signature=<signature>&X-Amz-SignedHeaders=host
*/
```

### Upload string or binary data or stream

```javascript
Expand Down
19 changes: 16 additions & 3 deletions src/Bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export class Bucket {
}

/**
* Returns the signed URL of an object. This method does not check whether the object exists or not.
* Returns the signed URL of an object used to perform a "GET" action.
* This method does not check whether the object exists or not.
* @param objectKey
* @param expiresIn Expiration time in seconds.
*/
Expand All @@ -111,8 +112,20 @@ export class Bucket {
Bucket: this.name,
Key: objectKey,
});
const signedUrl = await getSignedUrl(this.r2, obj, { expiresIn });
return signedUrl;
return getSignedUrl(this.r2, obj, { expiresIn });
}

/**
* Returns the signed URL of an object used to perform a "PUT" action.
* @param objectKey
* @param expiresIn Expiration time in seconds.
*/
public async putObjectSignedUrl(objectKey: string, expiresIn: number): Promise<string> {
Copy link
Owner

Choose a reason for hiding this comment

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

Isn't this just the same as the GET one? Or is it used when you want to put an object and then retrieve the signed URL?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from the docs it seems like the GET and POST ones are different URLs. So it's used to generate a signed URL where you can PUT an object.

const obj = new PutObjectCommand({
Bucket: this.name,
Key: objectKey,
});
return getSignedUrl(this.r2, obj, { expiresIn });
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/R2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export class R2 {
});
}

/**
* Returns the S3 client instance. Should be used as a last resort if you need extra custom functionality.
* @note It is recommended to use other provided methods for specific operations instead of directly accessing the client.
*/
public __unsafe_getClient(): S3Client {
return this.r2;
}

/**
* Returns a `Bucket` object that represents the specified storage bucket.
* @param bucketName The name of the storage bucket.
Expand Down