Skip to content

Commit

Permalink
Add an equivalent of the uploadFile method to the stateless SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
mlejva committed Feb 19, 2024
1 parent aed8518 commit 75d3e91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/js-sdk/src/sandbox/stateless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ export async function downloadFile({ apiKey, sandboxID }: { sandboxID: string, a
await s.close()
return result
}

/**
* Uploads a file to a sandbox.
* @param apiKey API key
* @param sandboxID ID of the sandbox to which the file will be uploaded
* @param path Path where the file will be stored inside the sandbox
* @param content The content of the file as a byte array
*/
export async function uploadFile({ apiKey, sandboxID }: { sandboxID: string, apiKey?: string }, { path, content }: { path: string, content: Uint8Array }) {
const s = await Sandbox.reconnect({ apiKey, sandboxID })
await s.filesystem.writeBytes(path, content)
await s.close()
}
13 changes: 11 additions & 2 deletions packages/js-sdk/testground/stateless.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ async function main() {
})
console.log('...command finished')

console.log('\n> Uploading file...')
await e2b.experimental_stateless.uploadFile({
apiKey,
sandboxID,
}, {
path: '/tmp/hello.txt',
content: new TextEncoder().encode('hello world'),
})
console.log('...file uploaded')

console.log('\n> Downloading file...')
const content = await e2b.experimental_stateless.downloadFile({
apiKey,
sandboxID,
}, {
path: '/etc/hosts'
path: '/tmp/hello.txt'
})
console.log('...downloaded file:\n', content.toString())

Expand All @@ -47,4 +57,3 @@ main()
.then(() => {
console.log('\n\nFinished')
})

0 comments on commit 75d3e91

Please sign in to comment.