当在 Python 中使用 Google Earth Engine 并通过服务帐户进行身份验证时,为什么导出到云端硬盘的内容为空?

发布于 2025-01-20 14:17:39 字数 1437 浏览 5 评论 0原文

我正在使用Python的Google Earth Engine获取Sentinel-2组成,然后将其下载到我的Google Drive。在进行手动身份验证时,一切正常工作:

ee.Authenticate()
ee.Initialize()

但是,由于我想在工作流程中使用我的代码,并且不要一直使用手动身份验证,因此我使用的是一个服务帐户,例如diced 在这里。它可以正常工作,我可以使用GEE而无需手动执行任何操作:

# get service account
service_account = '[email protected]'

# get credentials
credentials = ee.ServiceAccountCredentials(service_account, gee_secret.json)

ee.Initialize(credentials)

为了将文件导出到Google驱动器,我使用以下代码:

# export options
export_config = {
   'scale':10,
   'region':aoi, #aoi is a polygon
   'crs': 'EPSG:3031',
}
file_name = "test"

# export to drive
task = ee.Batch.Export.iamge.toDrive(image, file_name, **export_config)
task.start()

使用两个身份验证方法,此任务已成功完成(任务的状态已“完成”)。但是,只有在使用手动身份验证时,我才能在驱动器中看到我的导出图像。使用自动身份验证时,我的驱动器为空。

其他人已经问了一个类似的问题在这里。这里可能的想法是,图像文件已导出到服务帐户的Google驱动器,而不是我个人的Google Drive。但是,我不确定如何访问其他驱动器。

有人想知道如何解决此问题(=如何访问导出的文件?)。还是有另一个用于自动身份验证的解决方案,其中文件将在我的个人Google驱动器上使用?

I'm using the Google Earth Engine in Python to get a Sentinel-2 composition and download it to my google drive. When doing a manual authentication everything is working fine:

ee.Authenticate()
ee.Initialize()

However, as I want to use my code in a workflow and don't use all the time the manual authentication, I am using a service account, like described here. It works fine and I can use GEE without doing anything manually:

# get service account
service_account = '[email protected]'

# get credentials
credentials = ee.ServiceAccountCredentials(service_account, gee_secret.json)

ee.Initialize(credentials)

In order to export my File to Google Drive I'm using following code:

# export options
export_config = {
   'scale':10,
   'region':aoi, #aoi is a polygon
   'crs': 'EPSG:3031',
}
file_name = "test"

# export to drive
task = ee.Batch.Export.iamge.toDrive(image, file_name, **export_config)
task.start()

With both authentication methods this task is successfully finished (The status of the task is 'Completed'). However, only when using the manual authentication, I can see my exported image in my Drive. When using the automatic authentication, my Drive is empty.

Someone else already asked a similar question here. A possible idea here was that the image file is exported to the Google Drive of the service account and not to my personal Google Drive. However, I am not sure how to access this other Drive.

Does anyone have an idea how to solve this (=how to access the exported file?). Or have another solution for automatic authentication in which the file will be at my personal Google Drive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-27 14:17:39

非常感谢 DaImTo 和 Yancy Godoy 的提示!有了这些我就能找到解决办法。我将把它发布在这里,这样它可能对其他人也有用。

事实上,导出到 Google Drive 的效果非常好,但是它没有导出到我个人的 Google Drive,而是导出到服务帐户的 Google Drive。因此,为我的服务帐户添加对 Google Drive 的访问权限非常重要(请参阅 这里)。

您可以在下面找到完整的工作流程。对于下载,我使用 PyDrive

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials

# this file contains the E-mail adress of the service account
with open(key_path + '/service_worker_mail.txt', 'r') as file:
   service_account_file = file.read().replace('\n', '')

# get the credentials
credentials = ee.ServiceAccountCredentials(service_account_file, key_path + "/" + "gee_secret.json")

# authenticate and initialize Google Earth Engine
ee.Initialize(credentials)

#  The following operations of Google Earth Engine, including the Export to the Drive
# ...
# ...
# ...

# authenticate to Google Drive (of the Service account)
gauth = GoogleAuth()
scopes = ['https://www.googleapis.com/auth/drive']
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(key_path + "/" + "gee_secret.json", scopes=scopes)

drive = GoogleDrive(gauth)

# get list of files
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
    for file in file_list:

        filename = file['title']

        # download file into working directory (in this case a tiff-file)
        file.GetContentFile(filename, mimetype="image/tiff")

        # delete file afterwards to keep the Drive empty
        file1.Delete()


Many thanks to the hints of DaImTo and Yancy Godoy! With these I could find a solution. I will post it here, so that it is maybe useful for others as well.

Indeed the export to the Google Drive worked perfectly, however it was not exported to my personal Google Drive, but to the Google Drive of the service account. It was therefore important to add the access to Google Drive for my service account (see here).

In the following you can find a complete workflow. For the downloading I am using PyDrive

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials

# this file contains the E-mail adress of the service account
with open(key_path + '/service_worker_mail.txt', 'r') as file:
   service_account_file = file.read().replace('\n', '')

# get the credentials
credentials = ee.ServiceAccountCredentials(service_account_file, key_path + "/" + "gee_secret.json")

# authenticate and initialize Google Earth Engine
ee.Initialize(credentials)

#  The following operations of Google Earth Engine, including the Export to the Drive
# ...
# ...
# ...

# authenticate to Google Drive (of the Service account)
gauth = GoogleAuth()
scopes = ['https://www.googleapis.com/auth/drive']
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(key_path + "/" + "gee_secret.json", scopes=scopes)

drive = GoogleDrive(gauth)

# get list of files
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
    for file in file_list:

        filename = file['title']

        # download file into working directory (in this case a tiff-file)
        file.GetContentFile(filename, mimetype="image/tiff")

        # delete file afterwards to keep the Drive empty
        file1.Delete()


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文