首页 \ 问答 \ 如何使用boto使用python监控AWS S3存储桶?(How to monitor a AWS S3 bucket with python using boto?)

如何使用boto使用python监控AWS S3存储桶?(How to monitor a AWS S3 bucket with python using boto?)

我可以访问S3存储桶。 我不拥有水桶。 我需要检查是否有新文件添加到存储桶中以进行监控。

我看到桶可以触发事件,并且可以利用Amazon的Lambda来监视和响应这些事件。 但是,我无法修改存储桶的设置以允许此操作。

我的第一个想法是筛选所有文件并获取最新文件。 但是,该存储桶中存在大量文件,这种方法效率极低。

具体问题:

  1. 有没有办法有效地获取存储桶中的最新文件?
  2. 有没有办法使用boto监控上传到存储桶?

不太具体的问题:

  • 你会怎么解决这个问题? 假设你必须在桶中获取最新文件并打印出它的名字,你会怎么做?

谢谢!


I have access to a S3 bucket. I do not own the bucket. I need to check if new files were added to the bucket, to monitor it.

I saw that buckets can fire events and that it is possible to make use of Amazon's Lambda to monitor and respond to these events. However, I cannot modify the bucket's settings to allow this.

My first idea was to sift through all the files and get the latest one. However, there are a lot of files in that bucket and this approach proved highly inefficient.

Concrete questions:

  1. Is there a way to efficiently get the newest file in a bucket?
  2. Is there a way to monitor uploads to a bucket using boto?

Less concrete question:

  • How would you approach this problem? Say you had to get the newest file in a bucket and print it's name, how would you do it?

Thanks!


原文:https://stackoverflow.com/questions/33551143
更新时间:2022-04-28 18:04

最满意答案

您需要在单例中创建记录,在Ember的情况下,它将是控制器或服务,然后在模型钩子中返回该记录。 看看这个旋转工作的例子。 您需要的代码的快速示例:

//services/persistance-helper.js
export default Ember.Service.extend({

  store: Ember.inject.service(),

  rememberRecord(type, record){
    this.set(type,record)
  },

  retrieveRecord(type){
    const record = this.get(type)
    console.log('record:',record)
    if(record){
      return record
    }else{
      return this.get('store').createRecord(type)
    }
  },

  removeRecord(type){
    this.set(type,null)
  }
})

//routes/post.js
export default Ember.Route.extend({

  persistanceHelper: Ember.inject.service(),

  model(){
    return this.get('persistanceHelper').retrieveRecord('post')
  },

  actions:{

    willTransition(){
      console.log()
      if(this.currentModel.get('dirtyType') === 'created'){
        this.get('persistanceHelper').rememberRecord('post',this.currentModel)
      }else{
        console.log('else:',this.currentModel.get('dirtyType'))
        console.log('record was saved ?')
      }
    },

    save(){
      this.currentModel.save().then((record)=>{
            this.get('persistanceHelper').removeRecord('post')
      })
    }
  }

})

我认为代码足够描述,不能添加更长的解释:)


You'll need to create the record in a singleton, which will be either a controller or service in Ember's case, and then return that record in your model hook. Have a look at this twiddle for a working example. A quick example of the code you'll need:

//services/persistance-helper.js
export default Ember.Service.extend({

  store: Ember.inject.service(),

  rememberRecord(type, record){
    this.set(type,record)
  },

  retrieveRecord(type){
    const record = this.get(type)
    console.log('record:',record)
    if(record){
      return record
    }else{
      return this.get('store').createRecord(type)
    }
  },

  removeRecord(type){
    this.set(type,null)
  }
})

//routes/post.js
export default Ember.Route.extend({

  persistanceHelper: Ember.inject.service(),

  model(){
    return this.get('persistanceHelper').retrieveRecord('post')
  },

  actions:{

    willTransition(){
      console.log()
      if(this.currentModel.get('dirtyType') === 'created'){
        this.get('persistanceHelper').rememberRecord('post',this.currentModel)
      }else{
        console.log('else:',this.currentModel.get('dirtyType'))
        console.log('record was saved ?')
      }
    },

    save(){
      this.currentModel.save().then((record)=>{
            this.get('persistanceHelper').removeRecord('post')
      })
    }
  }

})

I think the code is descriptive enough to not add a longer explanation :)

相关问答

更多
  • 理想情况下,ID不应该改变。 但是,从语义上讲,你是正确的,这种方法看起来不正确。 有一个更干净的方法来做到这一点: save: function(contact) { contact.one('didCreate', this, function(){ this.transitionToRoute('contact', contact); }); this.get('store').commit(); } 更新2013-11-27(ED 1.0测试版): save: functio ...
  • 你必须在控制器内执行这样的操作: var controller = this; //After saving the form to the server: ...then(function(){ controller.set('YourFormData', ''); }); 您的表单数据必须是在控制器中具有绑定的属性,例如您的ID,名称和名称。 You have to do it inside the controller like this: var controller = this; //A ...
  • 您的把手中可能有糟糕的html(缺少结束标签等)。 Kingpin2k's answer above pointed me in the right direction. For anyone else stumbling across this question it boiled down to an error in my use of {{outlet}}. My original application template had the following structure: {{#if isA ...
  • 所以这是一个更复杂问题的一部分,它是: 如何在本地应用程序中检测到更改已从本地数据库同步到远程数据库? 这实际上并不难确定,但你只需要知道在哪里问。 基本上,您将要在远程CouchDB上监听更改,例如: new PouchDB('http://somewhere:5984/somedb') .changes({live: true}).on('change', function (change) { console.log('yo, got a change'); }); 该change对象将 ...
  • 而不是在适配器中进行转换,恕不是一个好的实践,恕我直言,你可以返回一个Error的实例,并在当前路由的error操作中处理它: App.UnauthorizedError // create a custom Error class App.ApplicationAdapter = DS.RESTAdapter.extend({ ajaxError: function(jqXHR) { var defaultAjaxError = this._super(jqXHR ...
  • 只是要指出其他任何一个问题,那就是他的属性是一个对象,它应该是一个数组。 从技术上讲,它也应该是复数。 不正确 { "users": { /// <---- users should be an array 正确 { "users": [ { "id": 1, "title": "Learn Ember.js", "is_completed": true }, { "id": 2, "title": "Le ...
  • 您需要在单例中创建记录,在Ember的情况下,它将是控制器或服务,然后在模型钩子中返回该记录。 看看这个旋转工作的例子。 您需要的代码的快速示例: //services/persistance-helper.js export default Ember.Service.extend({ store: Ember.inject.service(), rememberRecord(type, record){ this.set(type,record) }, retrieveRe ...
  • 大段引用 您可以更轻松地解决这个问题,这些是您可以遵循的步骤: 如果您希望表单开始锁定,则必须设置isLocked:true 删除lockForm函数 添加到您的输入disable属性。 这将根据`isLocked启用或禁用您的输入。 {{input type="text" class="form-control" id="" placeholder="" value=model.name disabled=isLocked}} 删除controller.send('toggleLock'); 来自mode ...
  • 你需要更新一些东西才能使它工作。 请参阅此文章以获取完整参考。 在模板方面,select标签需要在change事件上发送值。 我删除了value=""并将其替换为onchange事件。