【问题标题】:how to check for word in text file store in amazon s3 cloud storage against user input?如何根据用户输入检查亚马逊 s3 云存储中文本文件存储中的单词?
【发布时间】:2012-08-08 20:38:51
【问题描述】:

如何将存储在亚马逊 s3 云存储中的文本文件中的单词与搜索栏中的用户输入进行匹配,然后下载特定文件?

【问题讨论】:

  • 告诉我你是否没有想到做grep "keyword" s3://bucketname/*.* 的想法? :)
  • 你能再给我解释一下吗?对不起。

标签: java amazon-s3 amazon


【解决方案1】:

您在 Amazon S3 中的文件可以公开,然后使用 HttpURLConnection 进行访问,如下所示:

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;

    class Download
     {public static void main (String args[])
       {String u = "http://s3.amazonaws.com/Bucket/path/file.data";
        String d = download(u);
        System.err.println("Download "+u+"=\n"+d);
       }

      public static String download(String u)
       {final int B = 1024*1024; 

        try 
         {final URL url = new URL(u);
          final HttpURLConnection c = (HttpURLConnection)url.openConnection();
          c.connect();
          final int r = c.getResponseCode(); 
          if (r != 200) 
           {System.err.println("Got response code "+r+", expected 200, cannot download "+u);
            return null;
           }
          System.err.println("Content length="+c.getContentLength());

          final InputStream i = new BufferedInputStream(url.openStream(), B);
          final byte data[] = new byte[B];
          String o = "";

          for(int n = i.read(data); n != -1; n = i.read(data))
           {o += new String(data, 0, n);
           }

          i.close();
          return o;
         }
        catch (Exception e) 
         {System.err.println("Cannot download "+u+" due to "+e);
         }
        return null;
       }
     }

如果您无法公开这些文件,您将不得不采用更复杂的方法,即使用 Amazon IAM 创建用户、分配用户权限、生成密钥/秘密组合,然后使用适用于 Java 的 Amazon AWS 开发工具包记录在http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html。如果您需要有关该路线的帮助,请在此处回复。

【讨论】:

    【解决方案2】:

    checkout cloudbash.sh - 不仅仅是 grep,还有任何用于 S3 上数据的 linux 工具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-09
      • 2017-03-03
      • 2013-02-16
      • 2012-07-26
      • 2013-01-21
      • 2013-05-23
      • 2023-04-07
      • 2020-04-30
      相关资源
      最近更新 更多