如何检索通过 CollectionFS:S3 上传到 Amazon S3 的图像
How do I retrieve an image uploaded to Amazon S3 via CollectionFS:S3
在 Meteor 项目中,我使用 CollectionFS:S3 将图像保存到 S3 中的存储桶中。我看不到如何将它们加载回项目中。
我尝试使用 FSFile.url() 方法,如下面的代码所示,但它正在返回:
/cfs/files/images/e6jQ4Txgvb37GisEQ/imagename.jpg?token=XXX
我期待 Amazon S3 url,如 s3-eu-west-1.amazonaws.com/mybucket/myfolder/imagename.jpg
view.html:
{{#each images}}
URL: {{this.url}}
<img src="{{this.url}}" alt="thumbnail">
{{/each}}
view.js
images: function () {
return images.find();
}
我在 CollectionFS github 页面上看到的所有示例都使用 url 方法,所以不明白为什么我无法访问图像。
实际上是正确的。您从服务器获取 url,服务器将其重定向到 S3 上的正确文件。
换句话说,从概念上讲,服务器充当 S3 的代理,临时访问 S3 上的文件(因此令牌位于末尾)。
如果您检查图像收集服务器端 (meteor shell),您会发现它指向您的 S3 集合中的相对路径。
如果它没有正确显示图像,则可能是其他原因,但它应该与 cfs 提供的 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。
在 Meteor 项目中,我使用 CollectionFS:S3 将图像保存到 S3 中的存储桶中。我看不到如何将它们加载回项目中。
我尝试使用 FSFile.url() 方法,如下面的代码所示,但它正在返回: /cfs/files/images/e6jQ4Txgvb37GisEQ/imagename.jpg?token=XXX
我期待 Amazon S3 url,如 s3-eu-west-1.amazonaws.com/mybucket/myfolder/imagename.jpg
view.html:
{{#each images}}
URL: {{this.url}}
<img src="{{this.url}}" alt="thumbnail">
{{/each}}
view.js
images: function () {
return images.find();
}
我在 CollectionFS github 页面上看到的所有示例都使用 url 方法,所以不明白为什么我无法访问图像。
实际上是正确的。您从服务器获取 url,服务器将其重定向到 S3 上的正确文件。
换句话说,从概念上讲,服务器充当 S3 的代理,临时访问 S3 上的文件(因此令牌位于末尾)。
如果您检查图像收集服务器端 (meteor shell),您会发现它指向您的 S3 集合中的相对路径。
如果它没有正确显示图像,则可能是其他原因,但它应该与 cfs 提供的 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。