我正在制作一个软件,用于检测新文件是否从虚拟机实例上传到 Google Cloud Platform 的存储桶存储中。通过此命令,使用 Cloud Storage Fuse 将名为 images 的文件目录挂载到存储桶
gcsfuse cloud-storage-bucket ~/mystuff/images
每当文件上传到存储桶时,该文件也会出现在 images 目录中。我正在使用 Python Watchdog 包来检测是否创建了新文件
# -*- coding: utf-8 -*-
#!/bin/bash
import time
import TextDetector
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
DIR="~/mystuff/images"
class ExampleHandler(FileSystemEventHandler):
def on_created(self, event): # when file is created
# do something, eg. call your function to process the image
print("Got event for file %s" % event.src_path)
TextDetector.detect_text(event.src_path)
observer = Observer()
event_handler = ExampleHandler() # create event handler
# set observer to use created handler in directory
observer.schedule(event_handler, path=DIR)
observer.start()
# sleep until keyboard interrupt, then stop + rejoin the observer
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
问题是看门狗没有检测到任何内容,即使每次我在存储桶存储中上传某些内容时,图像目录中都会创建一个新文件。我也尝试过使用inotify,但结果也是一样。我在Windows平台本地尝试时,代码运行没有任何问题。其实我对 Ubuntu 还很陌生。谁能帮我解决这个问题吗?
最佳答案
您可能需要研究以下选项:
- Cloud Pub/Sub Notifications for Google Cloud Storage与 example in Python
- Cloud Functions Storage Tutorial
- Cloud Storage Object Change Notification
我建议不要在 FUSE 连接器之上构建并利用平台提供的 native 功能。
关于python - 在 Ubuntu 16.04 LTS 中检测与外部云存储同步的目录中的文件更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45874480/