首页 > 解决方案 > 为什么 awscli sync 在我的 Windows 服务中不起作用?

问题描述

我想编写一些代码,每 10 秒将本地目录同步到 s3 存储桶作为 Windows 服务。为此,我正在使用 aws cli sync,并正在使用os.system访问 aws CLI。

这是我的代码的一个简单示例:

x = 'aws s3 sync C:\path\to\folder s3://bucket-to-sync/'

def syncDirectory(*args)
   os.system(*args)

syncDirectory(x)

现在,此代码在常规 python 脚本中工作正常并按预期同步。但是,当我创建它并将其作为 Windows 服务运行时,它不会同步。

我已使用页面中的代码来创建和运行服务,并且只是将 main 函数替换为上面列出的代码。

class PythonCornerExample(SMWinservice):
    _svc_name_ = "ServiceTest"
    _svc_display_name_ = "Service Example"
    _svc_description_ = "Syncing dir to s3 bucket"

    def start(self):
        self.isrunning = True

    def stop(self):
        self.isrunning = False

    def main(self):
        while self.isrunning:
            os.system('aws s3 sync C:\path\to\folder s3://bucket-to-sync/')
            time.sleep(10)

if __name__ == '__main__':
    PythonCornerExample.parse_command_line()
   

我不确定为什么这不起作用,我尝试从命令提示符调试此代码,但它说它工作正常,但我的存储桶中没有显示任何内容。任何帮助表示赞赏。

标签: pythonamazon-web-servicesamazon-s3servicesynchronization

解决方案


推荐阅读