使用签名URL从aws s3获取图像

时间:2013-09-05 10:16:14

标签: android amazon-s3

我将图像从android上传到aws s3并从aws中找到生成的url并将其保存在我的服务器上但是过了一段时间我想用带有签名生成的URL获取图像我得到访问被拒绝。

任何人都可以帮我解决这个问题,我如何通过该网址从aws中获取图片?

1 个答案:

答案 0 :(得分:0)

尝试这种方式可能会对您有所帮助

public Bitmap getUrlContent(String urlstring) throws IOException
    {
        byte[] imageRaw = null;
        URL url = new URL(urlstring);

        Authenticator.setDefault(new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                    //your username and password here
                return new PasswordAuthentication(user, password.toCharArray());
            }});
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        urlConnection.setUseCaches(false);
        urlConnection.connect();
        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            try
            {
                InputStream in = new BufferedInputStream(
                        urlConnection.getInputStream());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int c;
                while ((c = in.read()) != -1)
                {
                    out.write(c);
                }
                out.flush();

                imageRaw = out.toByteArray();

                urlConnection.disconnect();
                in.close();
                out.close();
                return BitmapFactory.decodeByteArray(imageRaw, 0, imageRaw.length);
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }