Skip to content

Commit

Permalink
feat: Resumable Object Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Mar 21, 2024
1 parent 1990d9f commit c7cf96f
Show file tree
Hide file tree
Showing 15 changed files with 711 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-crews-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: Uploading Object support resumable
117 changes: 112 additions & 5 deletions examples/nextjs/src/components/object/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const CreateObject = () => {
</button>{' '}
<button
onClick={async () => {
if (!address || !file || !txHash) return;
if (!address || !file) return;
console.log(file);

const provider = await connector?.getProvider();
Expand Down Expand Up @@ -136,10 +136,83 @@ export const CreateObject = () => {
}
}}
>
2. upload
* upload
</button>{' '}
<button
onClick={async () => {
if (!address || !file) return;

const provider = await connector?.getProvider();
const offChainData = await getOffchainAuthKeys(address, provider);
if (!offChainData) {
alert('No offchain, please create offchain pairs first');
return;
}

const uploadRes = await client.object.uploadObject(
{
bucketName: createObjectInfo.bucketName,
objectName: createObjectInfo.objectName,
body: file,
resumableOpts: {
partSize: 1024 * 1024 * 16,
disableResumable: false,
},
},
{
type: 'EDDSA',
domain: window.location.origin,
seed: offChainData.seedString,
address,
},
);
console.log('uploadRes', uploadRes);

if (uploadRes.code === 0) {
alert('success');
}
}}
>
* resumable upload(part size is 16MB)
</button>
<br />
or uploaded by delegated agent
or uploaded by delegated agent :{' '}
<button
onClick={async () => {
if (!address || !file) return;

const provider = await connector?.getProvider();
const offChainData = await getOffchainAuthKeys(address, provider);
if (!offChainData) {
alert('No offchain, please create offchain pairs first');
return;
}

const uploadRes = await client.object.delegateUploadObject(
{
bucketName: createObjectInfo.bucketName,
objectName: createObjectInfo.objectName,
body: file,
delegatedOpts: {
visibility: VisibilityType.VISIBILITY_TYPE_PUBLIC_READ,
},
},
{
type: 'EDDSA',
domain: window.location.origin,
seed: offChainData.seedString,
address,
},
);
console.log('uploadRes', uploadRes);

if (uploadRes.code === 0) {
alert('success');
}
}}
>
* delegated upload
</button>{' '}
<button
onClick={async () => {
if (!address || !file) return;
Expand All @@ -156,7 +229,13 @@ export const CreateObject = () => {
bucketName: createObjectInfo.bucketName,
objectName: createObjectInfo.objectName,
body: file,
visibility: VisibilityType.VISIBILITY_TYPE_PUBLIC_READ,
delegatedOpts: {
visibility: VisibilityType.VISIBILITY_TYPE_PUBLIC_READ,
},
resumableOpts: {
partSize: 1024 * 1024 * 16,
disableResumable: false,
},
},
{
type: 'EDDSA',
Expand All @@ -172,7 +251,35 @@ export const CreateObject = () => {
}
}}
>
delegated upload
* delegated resumable upload (part size is 16mb)
</button>
<br />
<button
onClick={async () => {
if (!address) return;

const provider = await connector?.getProvider();
const offChainData = await getOffchainAuthKeys(address, provider);
if (!offChainData) {
alert('No offchain, please create offchain pairs first');
return;
}

const res = await client.object.getObjectUploadProgress(
createObjectInfo.bucketName,
createObjectInfo.objectName,
{
type: 'EDDSA',
domain: window.location.origin,
seed: offChainData.seedString,
address,
},
);

alert('progress: ' + res);
}}
>
get object's upload progress
</button>
<br />
<button
Expand Down
12 changes: 7 additions & 5 deletions packages/js-sdk/src/api/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { MsgToggleSPAsDelegatedAgentSDKTypeEIP712 } from '@/messages/greenfield/storage/MsgToggleSpAsDelegatedAgent';
import {
MsgToggleSPAsDelegatedAgentSDKTypeEIP712,
MsgCreateBucketSDKTypeEIP712,
MsgDeleteBucketSDKTypeEIP712,
MsgMigrateBucketSDKTypeEIP712,
MsgUpdateBucketInfoSDKTypeEIP712,
} from '@/messages/greenfield';
import { assertAuthType, assertStringRequire } from '@/utils/asserts/params';
import { UInt64Value } from '@bnb-chain/greenfield-cosmos-types/greenfield/common/wrapper';
import {
Expand Down Expand Up @@ -73,10 +79,6 @@ import {
import { SpClient } from '../clients/spclient/spClient';
import { TxClient } from '../clients/txClient';
import { METHOD_GET, NORMAL_ERROR_CODE } from '../constants/http';
import { MsgCreateBucketSDKTypeEIP712 } from '../messages/greenfield/storage/MsgCreateBucket';
import { MsgDeleteBucketSDKTypeEIP712 } from '../messages/greenfield/storage/MsgDeleteBucket';
import { MsgMigrateBucketSDKTypeEIP712 } from '../messages/greenfield/storage/MsgMigrateBucket';
import { MsgUpdateBucketInfoSDKTypeEIP712 } from '../messages/greenfield/storage/MsgUpdateBucketInfo';
import type {
GetBucketMetaRequest,
GetBucketMetaResponse,
Expand Down
Loading

0 comments on commit c7cf96f

Please sign in to comment.