【问题标题】:Can i search about some files stored in google bucket inside my react app?我可以在我的反应应用程序中搜索存储在谷歌存储桶中的一些文件吗?
【发布时间】:2020-07-28 22:58:52
【问题描述】:

我有一个反应应用程序,将一些文件存储在谷歌云“Bucket”中,所以我想知道是否可以搜索存储在我的 React 应用程序内的“Bucket”中的一些文件,我不知道确切的名称是什么其中,我可以这样做吗? 如果是,以什么方式? 如果您有任何教程,我将不胜感激。

我所说的搜索是这个列表和过滤器:

提前致谢。

【问题讨论】:

    标签: reactjs google-app-engine google-cloud-platform google-cloud-storage


    【解决方案1】:

    “搜索”是什么意思?如果您已经知道要查找的名称,可以尝试打开该文件。如果失败,它要么不存在,要么你没有打开它的权限。

    如果您想在打开之前查看它是否存在,这应该为您指明正确的方向:

    from google.cloud import storage
    client = storage.Client()
    
    blobs = client.list_blobs('your_default_bucket')
    
    filenames = []
    for blob in blobs:
        filenames.append(blob.name)
        
    print(filenames)
    
    file_exists = 'my_file.csv' in filenames
    
    print(f"file_exists: {file_exists}")
    

    【讨论】:

    • 感谢您的帮助,我更新了我的问题,我的意思是当我不确定文件的确切名称时,所以我需要一些文件建议取决于我在搜索栏中写的内容。
    【解决方案2】:

    对于这种情况,最好使用第 3 部分库。 react-autosuggest 可以满足您的需求。

    基本用法:

    import Autosuggest from 'react-autosuggest';
     
    // Imagine you have a list of languages that you'd like to autosuggest.
    const files = [
      {
        name: 'file1'
      },
      {
        name: 'file2'
      },
      ...
    ];
     
    // Teach Autosuggest how to calculate suggestions for any given input value.
    const getSuggestions = value => {
      const inputValue = value.trim().toLowerCase();
      const inputLength = inputValue.length;
     
      return inputLength === 0 ? [] : languages.filter(lang =>
        lang.name.toLowerCase().slice(0, inputLength) === inputValue
      );
    };
     
    // When suggestion is clicked, Autosuggest needs to populate the input
    // based on the clicked suggestion. Teach Autosuggest how to calculate the
    // input value for every given suggestion.
    const getSuggestionValue = suggestion => suggestion.name;
     
    // Use your imagination to render suggestions.
    const renderSuggestion = suggestion => (
      <div>
        {suggestion.name}
      </div>
    );
     
    class Example extends React.Component {
      constructor() {
        super();
     
        // Autosuggest is a controlled component.
        // This means that you need to provide an input value
        // and an onChange handler that updates this value (see below).
        // Suggestions also need to be provided to the Autosuggest,
        // and they are initially empty because the Autosuggest is closed.
        this.state = {
          value: '',
          suggestions: []
        };
      }
     
      onChange = (event, { newValue }) => {
        this.setState({
          value: newValue
        });
      };
     
      // Autosuggest will call this function every time you need to update suggestions.
      // You already implemented this logic above, so just use it.
      onSuggestionsFetchRequested = ({ value }) => {
        this.setState({
          suggestions: getSuggestions(value)
        });
      };
     
      // Autosuggest will call this function every time you need to clear suggestions.
      onSuggestionsClearRequested = () => {
        this.setState({
          suggestions: []
        });
      };
     
      render() {
        const { value, suggestions } = this.state;
     
        // Autosuggest will pass through all these props to the input.
        const inputProps = {
          placeholder: 'Type a programming language',
          value,
          onChange: this.onChange
        };
     
        // Finally, render it!
        return (
          <Autosuggest
            suggestions={suggestions}
            onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
            onSuggestionsClearRequested={this.onSuggestionsClearRequested}
            getSuggestionValue={getSuggestionValue}
            renderSuggestion={renderSuggestion}
            inputProps={inputProps}
          />
        );
      }
    }
    

    查看演示 here also

    【讨论】:

    • 感谢您的帮助,这个库可以处理存储在我的云中而不是本地存储中的文件吗?
    • 要访问您的数据,您需要某种后端服务器来将这些数据提供给您的 React 应用程序。您无法直接从客户端“react”获取此类数据。
    • 我装了一个小型服务器来使用 {Express API, fetch API} 将文件从客户端上传到云中的反应应用程序,但我不使用任何后端工具来存储数据,所以我可以添加一些东西到我的服务器在云中搜索还是我不能?因为我不想用另一个云数据库,很抱歉打扰你,我对这个领域的了解很简单。
    猜你喜欢
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    相关资源
    最近更新 更多