【问题标题】:Fetching the image from aws s3 with signed url使用签名的 url 从 aws s3 获取图像
【发布时间】:2023-03-09 17:42:01
【问题描述】:

我将图像从 android 上传到 aws s3 并从 aws 中找到生成的 url 并将其保存在我的服务器上,但一段时间后,当我想使用签名生成的 url 获取图像时,我被拒绝访问。

谁能帮我解决这个问题,我如何通过该 url 从 aws 获取图像?

【问题讨论】:

  • 因为存储在服务器文件夹中的图像受密码保护,所以当您从服务器下载图像时,您需要传递用户名和密码以及标题
  • 感谢您的即时回复。请你给我一些代码sinnepet或任何参考

标签: android amazon-s3


【解决方案1】:

试试这个方法可能对你有帮助

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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2014-07-28
    • 2017-10-15
    相关资源
    最近更新 更多