gpt4 book ai didi

node.js - 如何在云存储node.js客户端中从url上传文件

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:08 25 4
gpt4 key购买 nike

通过 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();
});
});
}

关于node.js - 如何在云存储node.js客户端中从url上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59908379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com