通过 Google Cloud Storage 版本 1,我成功使用以下功能上传文件。它将文件存储在临时目录中的唯一位置以供进一步处理。
Cloud Storage 版本 4 似乎不再接受 URL 作为源。它会提示该文件不存在。
import { File } from "@google-cloud/storage";
import { join, parse } from "path";
import { generate } from "shortid";
import { URL } from "url";
import { getBucket } from "./helpers";
/**
* This function takes a HTTP URL and uploads it to a destination in the bucket
* under the same filename or a generated unique name.
*/
export async function uploadFile(
url: string,
destinationDir: string,
options: { generateUniqueFilename: boolean } = {
generateUniqueFilename: true
}
): Promise<File> {
try {
const pathname = new URL(url).pathname;
const { ext, base } = parse(pathname);
let destination = join(destinationDir, base);
if (options.generateUniqueFilename) {
const shortId = generate();
destination = join(destinationDir, `${shortId}${ext}`);
}
const bucket = await getBucket();
const [file] = await bucket.upload(url, {
destination,
public: true
});
console.log(`Successfully uploaded ${url} to ${destination}`);
return file;
} catch (err) {
throw new Error(
`Failed to upload ${url} to ${destinationDir}: ${err.message}`
);
}
}
当前版本如何解决这个问题?我似乎找不到太多这方面的信息。使用 gsutil 对我来说不是一个选择。我需要将 URL 传递给云函数并从那里上传。
这就是我最终得到的结果:
import { File } from "@google-cloud/storage";
import { join, parse } from "path";
import { generate } from "shortid";
import { URL } from "url";
import { getBucket } from "./helpers";
import * as request from "request";
/**
* This function takes a http url and uploads it to a destination in the bucket
* under the same filename or a generated unique name.
*/
export async function uploadFile(
url: string,
destinationDir: string,
options: { generateUniqueFilename: boolean } = {
generateUniqueFilename: true
}
) {
console.log("Upload file from", url);
const pathname = new URL(url).pathname;
const { ext, base } = parse(pathname);
let destination = join(destinationDir, base);
if (options.generateUniqueFilename) {
const shortId = generate();
destination = join(destinationDir, `${shortId}${ext}`);
}
const bucket = await getBucket();
return new Promise<File>((resolve, reject) => {
const file = bucket.file(destination);
const req = request(url);
req.pause();
req.on("response", res => {
if (res.statusCode !== 200) {
return reject(
new Error(
`Failed to request file from url: ${url}, status code: ${res.statusCode}`
)
);
}
req
.pipe(
file.createWriteStream({
resumable: false,
public: true,
metadata: {
contentType: res.headers["content-type"]
}
})
)
.on("error", err => {
reject(
new Error(
`Failed to upload ${url} to ${destinationDir}: ${err.message}`
)
);
})
.on("finish", () => {
console.log(`Successfully uploaded ${url} to ${destination}`);
resolve(file);
});
req.resume();
});
});
}
我是一名优秀的程序员,十分优秀!