在我的节点应用程序中,当我尝试使用来自https://github.com/fsouza/fake-gcs-server的假GCS桶映像时。我必须使用假的gcs桶,因为我们不能公开访问我们的项目GCS桶,即使使用密钥。我遇到了几个问题。
docker-compose.yml
version: '3.9'
networks:
storage-network:
driver: bridge
db-network:
driver: bridge
frontend-network:
driver: bridge
services:
api:
container_name: node_api
build:
context: ../node-app/
dockerfile: Dockerfile.dev
ports:
- 8080:8080
depends_on:
- postgres
restart: on-failure
volumes:
- ../node-app/:/app/
networks:
- storage-network
environment:
# Port
PORT: 8080
storage:
container_name: fake-gcs-server
image: fsouza/fake-gcs-server
restart: always
ports:
- 8083:8083
volumes:
- ./gcs_bucket:/data
command: ["-scheme", "http", "-port", "8083", "-external-url", "http://[::]:8083", "-backend", "memory"]
networks:
- storage-network
在这个GCS桶中上传文件的NodeJs函数。
exports.testupload = async (req, res) => {
const bucketName = 'testBucket';
try {
await processFile(req, res);
const storage = new Storage({
apiEndpoint: "http://<ip_address_like_192.168..>:8083",
projectId: "test",
});
const bucket = storage.bucket(bucketName);
const blob = bucket.file(req.file.originalname);
const blobStream = blob.createWriteStream({
resumable: false,
});
blobStream.on("error", (err) => {
res.status(500).send({ message: err.message });
});
blobStream.on("finish", async (data) => {
res.status(200).send({
message: "Uploaded the file successfully: " + req.file.originalname,
url: publicUrl,
});
});
blobStream.end(req.file.buffer);
} catch (err) {
console.log(err);
if (err.code == "LIMIT_FILE_SIZE") {
return res.status(500).send({
message: "File size cannot be larger than 2MB!",
});
}
res.status(500).send({
message: `Could not upload the file: ${req.file.originalname}. ${err}`,
});
}
}
当我运行代码并检查。我总是得到http代码200,也就是成功。但是,当我查看映射文件夹gcs_bucket时,我看到它中没有文件。
从fake_gcs_server的码头映像登录控制台:
fake-gcs-server | time="2022-03-12T11:38:42Z" level=info msg="192.168.80.1 - - [12/Mar/2022:11:38:42 +0000] \"POST /upload/storage/v1/b/testbucket/o?uploadType=multipart&name=test_1647085120709.txt HTTP/1.1\" 200 476"
在另一个类似的例子中,我能够从这个假的GCS桶中读取文件。但我面临以下两个问题。
的ipv4 ipaddress。
请让我知道我在哪里做错误。
发布于 2022-07-12 17:38:01
如果使用docker-compose exec storage sh
检查容器内部,您将看到存储数据在/storage
中,而不是/data
中。
因此,您可以添加卷- ./.storage:/storage
来检查上传的文件。
(如果在运行时指定-backend memory
,则不要设置标志,因为您将无法看到这些文件。)
开始的ipv4 ipaddress。
试试http://storage:8083
。
https://stackoverflow.com/questions/71449424
复制相似问题