Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 45 additions & 4 deletions __tests__/commands/copy-cross-bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { B2Simulator } from '@backblaze-labs/b2-sdk/simulator'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { copyCommand } from '../../src/commands/copy.ts'
import { uploadCommand } from '../../src/commands/upload.ts'
import { makeInputs } from '../_helpers.ts'
import { MULTIPART_PART_SIZE, makeInputs } from '../_helpers.ts'

interface Fixture {
workDir: string
Expand All @@ -15,8 +15,10 @@ interface Fixture {
client: B2Client
}

async function makeFixture(): Promise<Fixture> {
const sim = new B2Simulator()
async function makeFixture(
simOptions: { minimumPartSize?: number; recommendedPartSize?: number } = {},
): Promise<Fixture> {
const sim = new B2Simulator(simOptions)
const client = new B2Client({
applicationKeyId: 'test-key-id',
applicationKey: 'test-key',
Expand All @@ -38,7 +40,10 @@ async function makeFixture(): Promise<Fixture> {
describe('copy command (cross-bucket)', () => {
let fx: Fixture
beforeEach(async () => {
fx = await makeFixture()
fx = await makeFixture({
minimumPartSize: MULTIPART_PART_SIZE,
recommendedPartSize: MULTIPART_PART_SIZE,
})
})
afterEach(async () => {
await rm(fx.workDir, { recursive: true, force: true })
Expand Down Expand Up @@ -78,6 +83,42 @@ describe('copy command (cross-bucket)', () => {
expect(remoteDest.files.some((f) => f.fileName === 'archive/app.tar.gz')).toBe(true)
})

it('copies a large file from one bucket to another', async () => {
const local = join(fx.workDir, 'large-cross.bin')
await writeFile(local, 'x'.repeat(MULTIPART_PART_SIZE * 3))
await uploadCommand(
fx.sourceBucket,
makeInputs('upload', {
bucket: 'src-bucket',
source: local,
destination: 'releases/v1/large.bin',
partSize: MULTIPART_PART_SIZE,
}),
)

const result = await copyCommand(
fx.client,
fx.destBucket,
makeInputs('copy', {
bucket: 'dest-bucket',
sourceBucket: 'src-bucket',
source: 'releases/v1/large.bin',
destination: 'archive/large.bin',
}),
)

expect(result.sourceBucket).toBe('src-bucket')
expect(result.destinationBucket).toBe('dest-bucket')
expect(result.size).toBe(MULTIPART_PART_SIZE * 3)

const remoteDest = await fx.destBucket.listFileNames({ prefix: 'archive/' })
const copiedFile = remoteDest.files.find((f) => f.fileName === 'archive/large.bin')
expect(copiedFile?.contentLength).toBe(MULTIPART_PART_SIZE * 3)

const remoteSrc = await fx.sourceBucket.listFileNames({ prefix: 'archive/' })
expect(remoteSrc.files.some((f) => f.fileName === 'archive/large.bin')).toBe(false)
})

it('falls back to same-bucket when source-bucket is unset', async () => {
const local = join(fx.workDir, 'same.txt')
await writeFile(local, 'same-bucket')
Expand Down
3 changes: 3 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35795,6 +35795,9 @@ async function copyCommand(client, destinationBucket, inputs, signal) {
? await destinationBucket.copyLargeFile({
sourceFileId: hit.fileId,
fileName: destination,
...(sourceBucketName !== destinationBucket.name
? { destinationBucketId: destinationBucket.id }
: {}),
...(signal !== undefined ? { signal } : {}),
})
: await destinationBucket.copyFile({
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/commands/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export async function copyCommand(
? await destinationBucket.copyLargeFile({
sourceFileId: hit.fileId,
fileName: destination,
...(sourceBucketName !== destinationBucket.name
? { destinationBucketId: destinationBucket.id }
: {}),
...(signal !== undefined ? { signal } : {}),
})
: await destinationBucket.copyFile({
Expand Down
Loading