【问题标题】:Display Google Drive Image In RShiny Using Service Account使用服务帐户在 RShiny 中显示 Google Drive 图像
【发布时间】:2023-02-15 13:54:39
【问题描述】:

感觉我可能在这里遗漏了一些东西。我希望使用 url 在闪亮的应用程序中显示来自我的谷歌驱动器的图像。我计划使用服务帐户从我的驱动器访问图像。我为此准备的简单代码如下。文件ID在此处的url中留空。

library(shiny)
library(tidyverse)
library(googledrive)

drive_auth(path = "client-secret.json")

ui <- fluidPage(
  h4("Embedded image"),
  uiOutput("img")
)

server <- function(input, output, session) {
  output$img <- renderUI({
    tags$img(src = "https://drive.google.com/uc?export=view&id=   #image_id#   ") 
  })
}

shinyApp(ui, server)

如果我将其公开,我正在使用的链接就可以了;但是由于安全问题,我想我可以在我的谷歌项目中使用我的服务帐户访问私人链接。我使用 similar approach to this 访问过很多次工作表。所以我真的认为这很简单

到目前为止我做了什么:

  • 将 Google Drive API 添加到我的项目中
  • 创建服务帐户并授予其编辑权限
  • 共享访问服务帐户的文件夹
  • 确保我使用https://drive.google.com/uc?export=view&id=访问图像
  • 尝试了 png 和 jpg 文件类型

这没有产生任何结果。为了检查我的理智,我继续并确保我可以以类似的方式使用该服务帐户访问 googlesheets。

gs4_auth(path = "client-secret.json")
URL <- #Sheet url
read_sheet(URL,sheet="Sheet1")

这与服务帐户的预期一样正常。我不是 100% 确定有什么区别,或者我是否可以加载私人图像(将服务帐户添加为文件夹和图像的编辑器)。

在我继续努力的同时,任何意见都在这里表示赞赏。

【问题讨论】:

  • 你从哪里得到那个端点?我认为你应该使用 https://www.googleapis.com/drive/v3/files/fileId/export
  • @DaImTo 该端点来自this post here。它似乎仍然适用于公众形象。查看您指定的方法here,它似乎在导出和离开 google 工作空间方面都存在问题。这意味着在尝试加载图像时我没有闪亮的运气

标签: r shiny google-api google-drive-api


【解决方案1】:

虽然这不是直接的方法,但您可以通过谷歌云存储桶和服务帐户访问驱动器文件,以在 Shiny 中显示它们。您需要在应用程序脚本和 Google Cloud Storage 中使用 Drive API。可以在 here 找到有关谷歌云存储和通过应用程序脚本上传图像的完整演练。从那里您需要一个与您的云项目关联的服务帐户。我授予了我的存储管理员和服务帐户令牌创建者权限。

将文件上传到 Google Cloud 存储桶后,您就可以访问 r 中的文件 以下是您如何向其他人解释代码的示例:

这是一个示例 Shiny 应用程序,它显示来自 Google Cloud Storage 存储桶的图像。该应用程序使用 googleCloudStorageR 包对图像文件进行身份验证和下载,使用 EBImage 包读取和显示图像。

#First, we define the necessary libraries and authenticate with our Google Cloud Storage account:


library(shiny)
library(googleCloudStorageR)
library(EBImage)

# Set the path to the service account key file
key_file <- "path/to/key/file.json"

# Authenticate with Google Cloud Storage
gcs_auth(key_file)
#Next, we define the user interface with a single output element for the image


ui <- fluidPage(
  displayOutput("outputId")
)
#Then, we define the server function that retrieves the image from the Google Cloud Storage bucket and renders it in the UI:


server <- function(input, output) {
  # Retrieve the image from the Google Cloud Storage bucket
  image_file <- gcs_get_object(bucket = "my-bucket", object_name = "my-image.png")
  
  # Convert the binary image data to an EBImage object
  image <- image_file %>% Image(colormode = "Color")
  
  # Display the image in the UI
  output$outputId <- renderDisplay({
    display(image, all = TRUE)
  })
}
#Finally, we run the app:

shinyApp(ui, server)
#This code can be modified to download and display other types of files from a Google Cloud Storage bucket, and to display them in a different way depending on the data type. 

从那里显示图像并可以与之交互。这不是对我自己的问题的直接回答,但它展示了如何做到这一点。存储桶设置为私有,图像应该以这种方式保持安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多