How I Built a Dropbox to Google Drive Transfer That Never Touches the User's Computer (Laravel)

2026-11-29来源 dev.toEnglish

WetoDrive started as a way to save WeTransfer links straight into Google Drive. People kept asking for Dropbox too, so I added Dropbox as a second source: pick a folder in your Dropbox, and it is copied into your Drive on the server, with nothing downloaded to your computer.

It turned out to be a small change, because of one decision made early on. Here is how it works, and the Drive and Dropbox details that cost me time.

A source is three questions

The WeTransfer pipeline already did the hard parts: building folders in Drive, streaming big files, tracking progress, enforcing plan limits. What it needed from a source boiled down to three methods:

public function resolvePageUrl(string $url): string; // what did the user give us? public function listItems(string $pageUrl): array; // which files, what names, what sizes? public function directLinkForItem(string $pageUrl, string $itemId): string; // a URL to fetch one file's bytes 

For WeTransfer, the "page URL" is a share link. For Dropbox it is just the path the user picked in the browser on the page. If Dropbox could answer those three, everything downstream would work unchanged.

Listing: recursive, and skip what has no bytes

foreach ($dropbox->listFolder($path, recursive: true) as $entry) { // Paper docs and the like have no bytes to download. if (($entry['.tag'] ?? '') !== 'file' || ! ($entry['is_downloadable'] ?? true)) { continue; } $files[] = $entry + [ 'relative' => $meta['name'] . '/' . ltrim(substr($entry['path_display'], strlen($root)), '/'), ]; } 

Two details here:

  • is_downloadable: false marks Dropbox Paper docs and similar entries. They are in the listing, but there is no file to fetch, so asking for one fails. Filtering them up front beats failing halfway through a folder.
  • Each path is prefixed with the picked folder's own name. Without that, picking Acme Shoot would spill its contents straight into the destination instead of arriving as an Acme Shoot folder.

The trick: temporary links

Dropbox has an endpoint that turns a file into a plain HTTPS URL:

public function temporaryLink(string $path): string { return $this->call('files/get_temporary_link', ['path' => $path])['link']; } 

The link is good for four hours and needs no token. That means a Dropbox file looks exactly like a WeTransfer download to the rest of the code, a URL that streams bytes, so directLinkForItem was a one-liner.

I used plain HTTP for Dropbox rather than an SDK. The whole integration is six endpoints, and Laravel's HTTP client with retries covers it.

Drive does not have paths

Google Drive has no concept of a path. A file has a name and a parent folder ID, and a / in a name is just a character. Upload a file named Acme Shoot/RAW/A001.mov and you get one file with that literal name.

So the pipeline creates each folder first (with a cache, so RAW is created once and not 200 times), then uploads only the basename into the right parent:

// Drive does not treat "/" as a path separator, so uploading the raw name // would make one flat file literally called "Shoot Day 1/Selects/x.mov". $item['name'] = basename(str_replace('\\', '/', $item['name'])); 

Big files stream, small files take a shortcut

Files of 1 GB and up never touch the disk. The download stream is read into a buffer, and every 10 MB goes to Drive as one chunk of a resumable upload:

while (! $stream->eof() || $buffer !== '') { $buffer .= $stream->read($this->chunkSize - strlen($buffer)); if (strlen($buffer) >= $this->chunkSize || $stream->eof()) { $chunk = substr($buffer, 0, $this->chunkSize); $buffer = substr($buffer, strlen($chunk)); $status = $media->nextChunk($chunk); // Google resumable upload } } 

(Simplified. The real loop also reports progress and handles the final short chunk.) Drive requires chunks of at least 256 KB, apart from the last one.

Files under 1 GB are downloaded to a temp file and uploaded from there, and the temp file is deleted in a finally. Files are processed one at a time, so the disk never holds more than one file under 1 GB, however big the folder is.

The bug that took longest to find was not in any of this. PHP-FPM's default memory_limit is 128M, and a 10 MB buffer plus the Google client's own copies of each chunk crashed large transfers right at the string concatenation. The stream path now sets memory_limit to 512M itself, so it is safe however it is called.

Asking Drive for as little as possible

WetoDrive signs in to Google with the drive.file scope only. That lets it create folders and files in your Drive, and read back only the files it created itself. It can never list or read anything else that is already in your Drive.

That was a product decision as much as a technical one. Asking for full Drive access to add files would be a hard sell, and the broad Drive scopes are restricted ones that need a yearly security assessment from Google.

The result

Connect Google, connect Dropbox, pick a folder, press Transfer. The folder arrives in Drive with its subfolders, the tab can be closed, and an email arrives when it is done. If you want to try it, it is at wetodrive.com/dropbox-to-google-drive. If you are weighing up the other ways to do it (download and re-upload, desktop apps, rclone), I compared them in how to transfer files from Dropbox to Google Drive without downloading.

Happy to go deeper on any of this in the comments.

原文:https://www.a8q9.cn/en/article/how-i-built-a-dropbox-to-google-drive-transfer-that-never-touches-the-user-s-com-590d59.html

相关文章