在 Meteor 项目中,我使用 CollectionFS:S3将图像保存到 S3 中的存储桶中。我无法看到如何将它们加载回项目中。
我尝试使用 FSFile.url() 方法,如下面的代码所示,但它返回: /cfs/files/images/e6jQ4Txgvb37GisEQ/imagename.jpg?token=XXX
我期待 Amazon S3 网址,如 s3-eu-west-1.amazonaws.com/mybucket/myfolder/imagename.jpg
查看.html:
{{#each images}}
URL: {{this.url}}
<img src="{{this.url}}" alt="thumbnail">
{{/each}}
View .js
images: function () {
return images.find();
}
我在 CollectionFS github 页面上看到的所有示例都使用 url 方法,因此不明白为什么我无法访问图像。
最佳答案
这对我来说是有效的。
在客户端(模板或客户端文件夹) 确保store变量之前已定义,并且具有相应的值。应与您在 S3 配置中定义的名称相同。示例:
store = {
bucket: 'my bucket name',
folder: 'folder name where files are stored'
}
定义S3Url函数:
FS.File.prototype.S3Url = function(storeName) {
var self = this;
var store = self.getCollection().storesLookup[storeName];
var urlHost = 'https://s3.amazonaws.com/';
var urlPath = [store.bucket, store.folder, this.copies[storeName].key].join('/');
return urlHost + urlPath;
}
使用后如下:
{{#each images}}
URL: {{this.S3Url}}
<img src="{{this.S3Url}}" alt="thumbnail">
{{/each}}
希望有帮助。
PS。确保在客户端中设置了 store.bucket 和 store.folder。关于javascript - 如何检索通过 CollectionFS :S3 上传到 Amazon S3 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28455407/